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

I Have created and deployed windows service, i want to fire this service in a time bound means for every 1 min or 5 mins like this,


Can any one can help me.


- Srinivas .P
Posted

You may use the Windows Task Scheduler for that (you have to use the NET command there, see, for instance "How do I start/stop a Windows Service from the command line?"[^].
In my opinion, your is a pretty strange scenario: usually services are meant to run continuously.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-14 16:14pm    
Voted 5. Indeed, Windows Task Scheduler is good to know and use for starting ordinary applications, but perhaps not other Services. Please see my answer for another advice elaborating your last sentence, which is really the key to the solution.
—SA
CPallini 3-Aug-14 16:38pm    
Thank you.
I disagree: the Windows Task Scheduler might indeed start services (OP scenario is odd because of the periodic restart).
Sergey Alexandrovich Kryukov 3-Aug-14 21:28pm    
I never said that it cannot. I said that this is not an adequate approach, especially for implementation of some regular actions (not even necessarily periodic). A Windows Service can itself provide this functionality, because it is supposed to run continuously anyway. I provided some better idea in my answer.
—SA
This is not the best idea, to star a service on time basis. The Window Services are really designed to be executed permanently, this is one of the main purposes of them. So, it's much better to keep some service running in a blocked state (not wasting any CPU time) and execute required method(s) either by timer, or in a separate thread with required delays in execution. When System.Threading.Thread.Sleep is called, the thread is also blocked (kept in a wait state) and does not spend any CPU time.

—SA
 
Share this answer
 
Comments
Srinivas Kumar Pasumarthi 5-Aug-14 12:41pm    
Kindly suggest me mechanism to run a piece of code to run continuously on time basis after computer starts
Sergey Alexandrovich Kryukov 5-Aug-14 14:00pm    
The best way would be create a thread in your service which would do the job you initially wanted to do by starting the service. It can call System.Threading.ThreadSleep and waste no CPU, when the call is returned, the thread is waken up. It can also check up time additionally by calling System.DateTime.Now. Do the job, call Sleep again according to the schedule, repeat ad infinitum...
—SA
Srinivas Kumar Pasumarthi 11-Aug-14 1:36am    
I appreciate your suggesation, followed mechanism specified by you, my requirement is fullfilled

Thank You.
Sergey Alexandrovich Kryukov 11-Aug-14 2:42am    
This is great. Will you then accept the answer formally (green "Accept" button)?
—SA

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