Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delay 5 min before starting a windows service?

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?

like image 285
Ehsan Zargar Ershadi Avatar asked Aug 10 '11 11:08

Ehsan Zargar Ershadi


People also ask

How do you start a delayed service?

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.

What is the difference between automatic and delayed start?

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.

What is delayed startup?

"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.


1 Answers

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
    }));
}
like image 142
Jalal Said Avatar answered Oct 27 '22 05:10

Jalal Said