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

I have a window service that should automatically start at particular time and stop automatically after some hours run everyday. i am handling in c#.net and i need guidance to achieve exactly what i need.


Thanks in Advance :)

What I have tried:

I have tried .net c# coding in windows service onstart and onstop event
Posted
Updated 1-Dec-17 21:38pm
Comments
Mehdi Gholam 2-Dec-17 2:58am    
Keep your service running and just schedule your work inside the service.
Perić Željko 2-Dec-17 5:21am    
Use Windows Task Sheduler Wizard. There is step by step explenation at Windows Help and Suport Center on how to use Windows Task Sheduler.

You can use the Windows Task Scheduler to do this, see: How to restart a windows service using Task Scheduler - Stack Overflow[^]
If you want to call the Task Scheduler from code I would advise calling it as a DOS command, the API libraries are very complex.
 
Share this answer
 
Comments
Maniraj.M 3-Dec-17 23:41pm    
Onstart event i wrote one function that will be called per hour from Onstart event. That function will check the time and if meets the required time it will navigate to the main function or else it will navigate to Onpause event. Once the end time comes the function will navigate to Onpause event and the next day it will navigate to Oncontinue event.. Will this work?
RickZeeland 4-Dec-17 2:40am    
No that won't work as the Onstart event is only called once on startup of your service. You could use a timer that calls your function every hour. Also don't try to do anything time consuming in the Onstart event as this is not allowed in services.
Maniraj.M 4-Dec-17 4:17am    
So finally i chose Task Scheduler. Thank you!
The OnStart and OnStop events are sent to your service when the service is started and stopped - handling them does not "make your service start" or "make your service stop" . They are there so your service can react appropriately - loading and saving progress perhaps - rather than to "get it working".

Instead, run your service at startup, and inside it check the time at regular intervals. If it's time to start processing, do so. If it's time to stop, cease processing.
 
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