Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Windows Service start as "Automatic (Delayed Start)"

Tags:

Scenario:

A WCF service running as a Windows Service. Account is "User".


What is done:

I have overridden the OnBeforeInstall in the projectinstaller to be able to set username and password from a config file.


What I would be able to do:

I'd like to be able to set the starttype as Automatic (Delayed Start)


What I have tried:

I put the following coderow in the overridden OnBeforeInstall

serviceInstaller1.StartType = ServiceStartMode.Automatic + 1;

Figured I would trick the ServiceStartMode enum into representing Automatic (Delayed Start), didn't work. Haven't tried anything more simply because I couldn't find anything to try.


What I have found on the net:

I found out that Automatic (Delayed Start) will be available in .NET 4, but that doesn't help me right now. MSDN I found out that DelayedAutoStart could be added to the service's configuration key, but this feels like a hack if I should do this from code. But maybe this is the only solution available for me at this point?

  • WS2008: Startup Processes and Delayed Automatic Start

Any ideas?

Robert Persson, Sweden

like image 363
Robert Persson Avatar asked Jan 28 '10 09:01

Robert Persson


People also ask

What is automatic delayed start in Windows service?

A service marked as Automatic (Delayed Start) will start shortly after all other services designated as Automatic have been started. In my experience, this means that they are started 1-2 minutes after the computer boots. The setting is most useful in lessening the "mad rush" for resources when a machine boots.

How do I get a service to start automatically in Windows?

From the desktop, click Start > Control Panel. Double-click Administration Tools. Double-click NetIQ Operations Center Auto-Restart Service. The Auto-Restart service automatically starts when Windows starts.

When you set a service to start automatically with delayed start How long is the delay?

One of the side effects of Automatic (Delayed Start) services is that they do start later in the boot cycle. The default delay is 120 seconds (2 minutes).


2 Answers

Now that .NET 4.0 is here:

serviceInstaller1.StartType = ServiceStartMode.Automatic; serviceInstaller1.DelayedAutoStart = true; 
like image 115
fiat Avatar answered Oct 14 '22 05:10

fiat


Your only other option is to use P/invoke to call ChangeServiceConfig2 with SERVICE_CONFIG_DELAYED_AUTO_START_INFO. But since you seem to be unwilling to add the registry entry, I doubt you would want to use P/invoke. There's no other way to do it from the .NET Framework (< 4.0).

like image 20
wj32 Avatar answered Oct 14 '22 05:10

wj32