Monthly Archives: June 2012

Windows Service Start up

Visual Studio 2010 has a very nice template for creating a windows service. It's fairly simple to just create it.

Click here to see tutorial on how to create and install a windows service.

Because a windows service cannot be running inside visual studio, it has to be running in a windows service management, therefore the service cannot be debugged in Visual Studio. The alternative option here is to attach a debugger on the service that is running.

Click here to see tutorial on how to attach a debugger on the service.

Few things to note is: The service is not viewable in the processes dialog box if it is not started. Which means you cannot attach your debugger unless you started the service. When the service start, it executes the Onstart() method. Once it started executing that method, you can see it in the process dialog box and attach debugger.

But what if you want to debug the Onstart() method?

You can use System.Threading, and use the static method Thread.sleep(x milisec); Make the service start wait for 1o sec, and attach the debugger during that 10 sec. But here comes another trick, the service management only gives arround 30-40 sec for the Onstart() to execute. If Onstart() takes too long, then error message appears saying your service is not responding. So the longer  you wait, the less time you give Onstart() to execute.

OnContinue()

This method does NOT mean after Onstart(), it will be executed. This method is only called after the service is paused and resumed.

A general structure of a service should be,

Onstart: Use threading, generate thread to start any processes. Do not put the process here, Onstart has very limited time to start!

OnStop: interrupt the thread, disable any thread, or timer. and dispose.