Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a windows service which is already deployed (using the setup and deployment project).
I need to start this service at a specific time (e.g. 6:00 a.m. everyday). And it should continue running till a fixed time (e.g. 7:00 p.m. same day).
The service should be stopped at the end time and should restart at the scheduled start time the next day.
Can anybody help me with some code to start the service programmatically(at the stipulated time)?

Thanks in advance!
Posted

Actually you just need a scheduled process, not the service. Still check the links if it helps.

.NET Windows Service Scheduler[^]
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/268a194c-747d-40b6-8b9e-903e44e47686[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 14:10pm    
Basically, I agree with you, my 5.
Nevertheless, I answered the question itself as it makes some sense, please see.
I credited you answer and explained why OP probably needs what you advised.
--SA
If you REALLY want to do it this way and rely on a scheduler, all you have to do is schedule two command lines to be run. The first at your specified start time and the second at your stop time:
net start servicename

net stop servicename


No code required...
 
Share this answer
 
v3
Change the code to have the service start and stop on its own. If you provide the start/stop time in a config file, you can customize the start/stop time by simply changing the file.

If cleverly written, you can have the service look for changes to the config file, and if detected, alter it's schedule without having to be stopped/restarted by the user.

EDIT ===========

Why was this one-voted? It's the most reliable solution because there are fewer moving parts than writing yet another app to control the service, and then tying it to the Windows scheduler.
 
Share this answer
 
v5
Comments
Dave Kreskowiak 29-Jun-11 19:51pm    
It makes perfect sense to me and it's the way I would write it.
You can use the class System.ServiceProcess.ServiceController to control the service programmatically from any .NET application. See http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx[^].

But what application will do this operation to be practical? I guess, some other service which you have to create. That's why Service Scheduler could be the best alternative, as Prerak advised.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900