如何以编程方式在 Windows 服务应用程序中创建暂停?

发布于 2024-11-05 05:15:56 字数 181 浏览 0 评论 0原文

我正在创建一个 Windows 服务应用程序,当发生系统错误、odbc 连接或丢失文件错误时,我希望以编程方式暂停该应用程序。我想知道是否有人知道该怎么做? Windows 服务应用程序使用 odbc 连接和数据读取器连接到 MS Access 数据库和 Oracle 表,因此我可能会处理这些错误,我只想允许用户暂停处理错误,如果/当它们发生时。

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 are 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-11-12 05:15:56
ServiceController service = new ServiceController(serviceName);

TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutValue);

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

...

然后重新启动,执行相同的操作,除了

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

您可以为任何您想要的状态执行此操作。通过谷歌搜索 SeviceController 查看 msdn 文档。这将是返回的第一个结果。

此外,您还需要处理服务中的 OnPause 和 OnContinue 事件。

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.

非要怀念 2024-11-12 05:15:56

你尝试过吗?

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

调整 1000 到 1000 次,无论您希望它在几秒钟内休眠多久。

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.

静待花开 2024-11-12 05:15:56

假设您的服务有一个检查数据的连续循环,请添加对外部源的暂停/继续命令检查。该源可以是消息队列(如 MSMQ)或数据库表。

我通过让我的服务不断检查表中的命令并在另一个表中报告其状态来实现类似的功能。当它收到启动命令时,它会在另一个线程上启动处理循环。停止命令会导致它向线程发出信号以正常退出。服务核心永不停止运行。

用户通过单独的应用程序与 UI 进行交互,使他们可以查看服务的状态并提交命令。由于应用程序通过数据库进行控制,因此不必在运行服务的同一台计算机上运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文