Like all applications, a Windows service has an entry point. In C#, this is called Main() and is a static method on some class. Inside the Main() function of your Windows service, you should have something akin to this:
ServiceBase[] ServicesToRun = new ServiceBase[] { new MyWindowsService() };
ServiceBase.Run( ServicesToRun );
In this example, MyWindowsService
is the name of the Windows service class to run and should be replaced with whatever your Windows service class name is.
When this code is executed in Main(), the default constructor for your Windows service class will be called, which looks something like this:
public MyWindowsService()
{
// service instance initialization goes here...
}
This is where you would initialize the MyWindowsService
instance.
Now, the MyWindowsService
class should be derived from System.ServiceProcess.ServiceBase
. If that is true, then you can override the OnStart() method, which is called when a Start command is sent to the service by the Service Control Manager.
protected override void OnStart(string[] args)
{
// things to do when starting the service...
}
Once the OnStart() function returns, your service is effectively running, i.e., started.
So, the question is where along this chain of events your delay is occurring - in Main(), the service constructor, or the OnStart() callback method. Have you tried debugging your service? An easy way to do this is to place the following line of code inside the Main() function of your service:
System.Diagnostics.Debugger.Break();
When you start the service, you will be prompted to select a debugger. Simply select a new instance of Visual Studio, and you'll jump right into the code at the point where this Break() call is made. You can debug from there, setting breakpoints in the pertinent places (constructor, OnStart()) to see where the hangup is occurring.