New Host

Due to a series of downtime and loooong delays fixing them, I'm bailing off of my current host and moving back to the wonderfull 1and1 - Just installing the features needed to run my sites the server is feeling much more snappy and responsive - lets hope its a good move!

If you can see this post, then your using the new updated DNS settings and are browsing NinjaMonkey on the new and improved Mamba!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 6/14/2010 at 8:00 PM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (5) | Post RSSRSS comment feed

CleggWorship

Currently rated 1.0 by 2 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 4/26/2010 at 5:14 PM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (5) | Post RSSRSS comment feed

Here's to blogs!

Currently I am eating a biscuit in honor of Blogs, a brand new one in specific, and how wonderful they are.

 

Here's to blogs.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 4/16/2010 at 11:17 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (14) | Post RSSRSS comment feed

Talking to Spotify

The Mission: Give the blue ThinkVantage button on my Thinkpad something usful to do

The Solution:  Make it pause Spotify

The Implementation:

This one comes in two parts, first of all we need to look at what makes the Blue ThinkVantage button tick.  Now, personally I dont get the point of that ThinkVantage menu dealio that comes up when you hit the blue button, so when I installed Windows 7 it wasnt something that got re-installed.  However, to get the button funcational at all you need to install the HotKey Manager (avaliable on the Lenovo site, I dont have a link im afraid as I've had it installed forever to get the volume keys working.  If you volume keys work, your all set, if they dont, you have to go find a driver before you continue I'm afraid) and then fire up The Registry.

 

Aww hell, I've always wanted to say this.

 

Warning: Playing with your registry can severly screw up your computer, children, life and any family pets.  Registry hacking is the start of a long slippery slope down to Heroin addiction and prostitution.  Or....some kind of disclaimer like that, basically - don't mess around in the registry, you'll probally brick your computer unless you know what you are doing.

 

So, we have totally ignored that warning and gone start/run/regedit and then navigated to HKLM/Software/IBM/TPHOTKEY.  Now, you may, or may not have the following key, so if you don't have it, create it - you want to go into the 8001 key, which apparently is the blue button.  Then you add a string value called "File" and give it the path to the program you want to fire off of the buton, so if you put "C:\\windows\\notepad.exe" (and it's a good idea to do that once to make sure it's working ok) when you hit the blue button, notepad will fire up.  Note the double backslashes in the path, and remember to use the old ~1 DOS style folder naming, so C:\Program Files beecomes C:\\Progra~1\\.

http://forum.notebookreview.com/showthread.php?s=409a0bf7627cccc6ee612fc3efe18838&t=171644

Ok, thats part one.  Now on to part B!

You need to write some code here, basically what we are after is a form which loads, finds and then fires a message to spotify telling it to play/pause, and then closes again. The following will work (sorry, its late I ant be bothered to code format this one):

 using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PlayPause
{
    public partial class Form1 : Form
    {
        private const int APPCOMMAND_PLAY_PAUSE = 14;
        private const int WM_APPCOMMAND = 0x0319;
        private System.Int32 iHandle;


        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("User32.dll")]
        public static extern int FindWindow(string strClassName,
                                                 string strWindowName);

        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(
            int hWnd,               // handle to destination window
            int Msg,                // message
            int wParam,             // first message parameter
            int lParam);            // second message parameter


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                iHandle = FindWindow("SpotifyMainWindow", null);
                int vCallReturn = SendMessage(iHandle, WM_APPCOMMAND, 0x00000000, APPCOMMAND_PLAY_PAUSE << 16);
                this.Close();
            }
            catch (Exception)
            {
                this.Close();
            }
        }
    }
}

What your doing is pulling in the methods you need from the Windows API, setting up your constants into integers, then on load you are using FindWindow and the Spotify main windows Class (you set the last parameter to blank because Spotify keeps changing it's title so you cant rely on that to identify the window) to get the window handle of Spotify, then sending the window a message of class WM_APPCOMMAND (The media keys commands) and the command itself, APPCOMMAND_PLAY_PAUSE (which we bitshift 16 places).  We then close the window.  I also have it set up to load minimised and not appear on the taskbar, so it dosnt look like a window has loaded at all.

You then tie the two steps toegether by pointing your registry key at the exe you just made  and wam, the Blue Button is pausing Spotify!

Here are the links I used to figure the above out - I really dont get the oppotunity to play with Native code much, being a web developer, so it was a fun explore of the Interop Services.

http://msdn.microsoft.com/en-us/library/ms646275%28VS.85%29.aspx

http://www.codeproject.com/KB/cs/wmp_pinvoke.aspx?msg=735041

http://www.microsoft.com/indonesia/msdn/pinvoke.aspx

http://www.codeproject.com/KB/wtl/WTLAppButtons.aspx?msg=2332697

http://kalshagar.wikispaces.com/Remote+controlling+Windows+media+player+in+C

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 4/14/2010 at 5:21 PM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (12) | Post RSSRSS comment feed

BlogSpam

One of the many template blogspam comments in the article two away from this - it's quite a funny read.  Note I was mocking the rediculous name of a song:

"I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Big thanks for the useful info i found on Alien Vampires."

Damn.....people are researching that eh?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 4/7/2010 at 4:07 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

The dreaded ThreadAbortException

Everyone has come across this one at least once (usually early on) in their ASP.NET career.

First, the scenario:

--> You have a bit of code that may go wrong and is very public, so you want to make sure all exceptions are handled.

--> You didnt read the manual and learn about Application_Error()

--> You put one big try/catch (Exception ex) { handle here; } around a block of code.

Ok, so life is now happy, and any errors are being caught....right?

 

right, infact life is very happy.  Ok, so now I want to redirect on a bit of conditional logic so we go:  Response.Redirect("newpage.aspx", true); and we redirect right?

Wrong.

Big fat ThreadAbortException being thrown off of the Response.Redirect, getting caught in our error handler, and 'things go wrong' - either it redirects but gets logged to your error logger, or your users get redirected to your error page (depending on your error handler, hopfully it's both) PLUS we cant see what it is in a debug session?  WTF? Bad?

 

Actually, it's not bad, it's the way ASP.NET makes sure your function stops processing - see, when you call Response.Redirect and pass 'true' in as the second parameter (or omit a second parameter it'll default to true) ASP.NET sends a 302 redirect to the client, and stops all further processing including your current routine, and any other event handlers in the queue to be run in that page lifecycle.  And how does it do that?  Thats right kids, it throws a ThreadAbortException.

 

Ok, so this here is a lesson on catching the base Exception method - and why I always tell people you NEVER, EVER DO IT.  I admit, there are times where you cant be bothered to go to MSDN and read up all the possible excepction types that can come from a function call and wrap it appropriately, so your lazy, bad programmer ass just catches Exception, but to be honest it should be a very very rare situation where you dont know what might happen and can test for those situations before the fact (one good example of when you can try/catch is the lack of Guid.TryParse - I posted a while back about that one) and make sure the Exceptions can't happen anyways.  Can you honestly tell me you can handle a StackOverflow in a way that allows the program to continue in a normal state?  What about an OutOfMemoryException?  No? Then why the hell are you catching them?  Let them ripple up to the top layer, it's NOT THAT BAD.

 

Ok, rant over.

 

Lets assume you HAVE to keep that ol' try/catch there, how can we allow Response.Redirect to be used within that block?  Modify your catch to read as follows, and feel bad about your life that you caught the Exception type:

catch (Exception ex)
{
   if (ex is System.Threading.ThreadAbortException)
       throw;

   Handler Error Here.
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 4/6/2010 at 11:49 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (7) | Post RSSRSS comment feed

Alien Vampires

I've been listening to alot of Industrial music recently on Last.FM - and one thing I love about it (apart from the music of course) are the names of the bands:

 



No joke.  While your reading this, go listen to a band called "Steel Panther", you'll love it, I promise.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 3/3/2010 at 2:33 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (35) | Post RSSRSS comment feed

I get too excited over new ways to work with generics

I do love a way to re-write code to make it even cleaner, so I'm a great fan of C#s generic List<T>.find() method as it allows me to search a list without the mess of a foreach loop and holding variable.

I do, however, always have to Google for the exact result if the condition is never met (so the thing we are searching for is never found), which, by the way, is default(T).

For example:

   1:  List<DateTime> dateList = generateDates();
   2:  DateTime dateWeWant = generateDateWeWant();
   3:  DateTime resultDate = dateList.find(delegate(DateTime searchDate){return (resultDate = dateWeWant);});
   4:  if (resultDate != default(DateTime))
   5:  {
   6:  //Found the date, work with it
   7:  else
   8:  {
   9:  //Not found the date, do something else (Don't you just love programmer grammar?)
  10:  } 

Which is all well and good, but there is an alternate syntax that gives the predicate an extra scrub and makes it look even prettier:

   1:  List<DateTime> dateList = generateDates();
   2:  DateTime dateWeWant = generateDateWeWant();
   3:  DateTime resultDate = dateList.find(testDate => testDate == dateWeWant );
   4:  if (resultDate != default(DateTime)
   5:  {
   6:  //Found the date, work with it
   7:  }
   8:  else
   9:  {
  10:  //Not found the date, do something else (Don&#39;t you just love programmer grammar?)</span>
  11:  } 

Is veeery niiiiiice.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 10/30/2009 at 5:36 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (43) | Post RSSRSS comment feed

Usfull web stuffs (triangles and color pallets)

Triangles in CSS you say?  Oh yes my friend....an interesting abuse of a feature: http://www.dinnermint.org/css/creating-triangles-in-css/  Hell, i didnt even know you could do the 4 color definition thing.

 

A bunch of tools - more specifically the color builder ones: http://webdesignledger.com/tools/15-free-online-tools-for-web-designers-on-a-budget

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 10/28/2009 at 4:53 PM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (126) | Post RSSRSS comment feed

Article on .NET Configuration Files

Right Here:  A really good basic overview, and it promises (I'm still reading it) some more advanced trickery.  It also has a link in it to a download of the Framework Src...which looks like the CLR source with a build environment :)

New Toys!

Article:

http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx

The Framework Src:

http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en

The song I'm listening to:

http://vampirefreaks.com/missnull

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Chris
Posted on: 10/23/2009 at 5:38 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (120) | Post RSSRSS comment feed