开发者

How to programmatically create pause in Windows Service app?

I am creating a Windows Service app that I would like to have programmatically pause when either a system error, odbc connection, or missing file error occur while . I was wondering if anyone knows how to do this? The Windows service app uses an odbc connection and datareader to connect to an MS Access database and an Oracle table, so there ar开发者_Python百科e the probable errors that I would be handling with those, I just want to allow a pause for the user handle the errors if/when they occur.


ServiceController service = new ServiceController(serviceName);

TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutValue);

service.Pause(); //or whatever you want here.
sevice.WaitForStatus(ServiceControllerStatus.Paused, timeout);

...

Then to restart, do the same thing except for

 service.Continue();
 sevice.WaitForStatus(ServiceControllerStatus.Running, timeout);

You can do this for any state you want. Check out the msdn documentation by googling SeviceController. It will be the first result returned.

Also, you will need to handle the OnPause and OnContinue events in your service.


Have you tried?

System.Threading.Thread.Sleep(1000); // sleep for 1 second

Adjust the 1000 to 1000 times however long you want it to sleep in seconds.


Assuming that your service has a continual loop that checks for data, add a check to an external source for pause/continue commands. This source can be a message queue like MSMQ or a database table.

I implemented something along like this by having my service continually check a table for commands, and reporting its status in another table. When it gets a start command it launches a processing loop on another thread. A stop command causes it to signal the thread to gracefully exit. The service core never stops running.

The user interacts via a separate app with a UI that lets them view the service's status and submit commands. Since the app does its control via a database it doesn't have to run on the same machine that the service is running on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜