Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Pause and Continue Windows Service By C#

i write a windows service by c# 4 and override some methods for it, like OnStart() and OnStop() and OnPause() and OnContinue() and OnShutdown().

i can start or stop this windows service, but i can not pause or continue it whether by programming or in services.msc. in other words, Start and Stop are Enable and Pause and Resume Options is Disable.

enter image description here

what is problem?

like image 334
Pezhman Parsaee Avatar asked Apr 27 '13 07:04

Pezhman Parsaee


People also ask

How do I run a Windows service continuously?

Thread object you create in the OnStart() callback. The _shutdownEvent field holds a system-level event construct that will be used to signal the thread to stop running on service shutdown. In the OnStart() callback, create and start your thread. You need a function named WorkerThreadFunc in order for this to work.

What is service pause?

When a service is paused, it can maintain internal state, including cached information or possibly even a queue of waiting work items. The service can then be resumed to pick up where it left off. If the service is stopped, internal state is discarded. Starting the service again should repeat all initialization.


1 Answers

You need to set the CanPauseAndContinue property to true as well. This is documented in OnContinue:

OnContinue is expected to be overridden when the CanPauseAndContinue property is true.

If CanPauseAndContinue is false, the SCM will not pass Pause or Continue requests to the service, so the OnPause and OnContinue methods will not be called even if they are implemented. In the SCM, the Pause and Continue controls are disabled when CanPauseAndContinue is false.

like image 129
Jon Skeet Avatar answered Oct 15 '22 23:10

Jon Skeet