I'm using .Net 4 to build a simple windows service , the problem happens when I want to make a 5 min delay before starting the service.
protected override void OnStart(string[] args)
{
// make a 5 min delay
// do something
}
but after few seconds the service stops and gives me a time out error (saw this in the event-log). what am I suppose to do?
It's of type REG_DWORD. Set the value of the key to the desired delay in milliseconds (e.g. 60000 for one minute). You will need to reboot for the change to take effect. Remember this change affects all Automatic (Delayed Start) services - this includes FireDaemon Pro and all other Windows services.
In short, services set to Automatic will start during the boot process, while services set to start as Delayed will start shortly after boot. Starting your service Delayed improves the boot performance of your server and has security benefits which are outlined in the article Adriano linked to in the comments.
"Delayed Startup" lets you set up any startup program to run one or more minutes later after Windows startup. This allows you to begin using the computer without waiting for Windows to load all startup applications. Some programs are loaded by Windows after it starts. Such programs are listed in window Startup.
Start your long running process in a new thread, so don't block the OnStart
method. example:
using System.Threading;
protected override void OnStart(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback((_) =>
{
Thread.Sleep(5 * 1000 * 60);//simulate 5 minutes work
// do something
}));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With