This Gadget for Windows Vista or Windows 7 gives your control over Windows Services like IIS/W3SVC or MSSQLServer. You can see if the Services are running, stop or restart them.
One strange thing that always hits me when I am fighting deadlines is that the closer they come the more I let myself drift away to spend time on some other non-important projects. I am sure that every programmer knows about it ..
Every now and then I give up to they call and instead of getting important things done I spend some time on something that I always wanted to do. This little gadget is the fruit of one of those fits of procrastination:

What is it good for ?
My Development machine runs under Windows Vista using a full blown IIS7 and SQL-Server. While I use both for my daily work I don’t want to have them running always. so I changed the configuration for the W3SVC and MSSQLServer Services from automatic to manual start and wrote me a batch file that starts the Services on the first run and stops them on the second run. That kind of worked but it was awfully slow and often I forgot if the Services where already running and stopped them instead of starting them.
Ok, one of those busy days I had enough of this crutch and thought it would be nice to have a little gadget that shows me the current state of my IIS and SQL-Server and allows me to start or stop the Services.
What sounded so easy finally took me more than 2 days to realize - but it was worth it and beside that, it was a lot of fun.
How it works.
Gadget programming is actually quite easy and straight forward, but has some peculiarities. one of my first step was to download a Visual Studio Gadget Template which gave me a basic overview about the structure of a gadget. A few researches on the Net revealed that some WMI Scripting and a little HTML should do the trick .. well yeah, sort of. The first problem was to find the gadget folder so that I could work on the gadget.html file itself instead of re-installing the gadget every time. Here it is:
C:\Users\YOUR_USERNAME\AppData\Local\Microsoft\Windows Sidebar\Gadgets
The second problem was more technical: Using WMI it is very easy to connect to a WMI Server and get access to the Services, but the html display is not updated immediately when you write to it. My first attempt was to request the current state of all registered Services in a loop and write the output into a table cell until the Serive is either running or stopped. Unfortunatly the IE Page did not reflect any changes, all I got was the first state ( while entering the loop ) and the exit state. Rats!
Now in a regular Windows Script you can simply use WScript.Sleep() to release a time slot and give the Browser page some time to refresh the display, but Windows gadgets run as HTML Applications (hta) and WScript.Sleep or WScript.Echo are properties of the WScript object, which can’t be created. Instead, the WScript object is automatically created - and only created - when you run a Windows Script Host (that is, WScript.exe or CScript.exe), not if you start a hta gadget.
Luckily there is a workaround for this problem using window.setTimeout to mimik WScript.Sleep, so instead of running in a closed loop we merely use setTimeout and recursive calls, and that lets Windows properly update our little Gadget display.
Installing the Gadget
The last hurdle to take was to create some kind of installation package – I thought.
In fact Microsoft was very very smart here, all you need to do is to pack all files of a gadget into a ZIP file and then rename the .zip extension to .gadget ! To install the gadget on your machine, simply double-click on the gadget archive and it installs itself in your gadget folder. Now that was easy, wasn't it ?
To use it, simply click on the Start and Stop buttons to start and stop the Services, and click on the header to refresh the Display - I did not want to have a timer running that constantly request the Service State.
You can add or remove Services on your own demand, take a look into gadget.html, all Services are declared inside the .Init() method.
Hope some of you will find this useful, enjoy, and let me know when you extend it !
Resources: