如何正确处置BackgroundWorkers

发布于 2024-09-03 19:46:46 字数 365 浏览 4 评论 0原文

我有一个运行 BackgroundWorker 的 Windows 服务,我想知道当我停止 Windows 服务时我是否在做正确的事情。

是否足够:

  1. BackgroundWorker1_DoWork 方法完成(我现在有一个 while 循环,执行一些任务)
  2. 将保存对 BackgroundWorker 的引用的变量设置为null

是否需要调用某种 Dispose() 方法(同样,Timer 类具有 Timer.Dispose ();)?

I've got a Windows Service that runs BackgroundWorker's, and I'm wondering if I'm doing the right thing when I stop my Windows Service.

Is it enough to:

  1. Let the BackgroundWorker1_DoWork method complete (I have a while loop in it now, doing some tasks)
  2. Set the variable that holds the reference to the BackgroundWorker, to null

Is there some kind of Dispose() method I need to call (In the same way the Timer class has Timer.Dispose();)?

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

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

发布评论

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

评论(4

剧终人散尽 2024-09-10 19:46:46

坦率地说,当您停止 Windows 服务时,这确实无关紧要。您的 win32 进程已终止,因此并不绝对需要清理 IDisposables。任何非托管资源都将具有仍将运行的终结器。

也就是说,当服务类处理时,有一个类级别的后台工作人员被处理是很正常的。没有必要,但保持干净总是好的。

Frankly, when you stop your windows service - it really doesn't matter. Your win32 process is terminated, so its not strictly necessary to clean up your IDisposables. Any unmanaged resources will have a finalizer that will still run.

That said, it's normal to have a class level background worker that gets disposed when e service class does. Not necessary, but it's always good to be clean.

温柔女人霸气范 2024-09-10 19:46:46

由于 BackgroundWorker 实现了 IDisposable,因此您应该在使用完它后将其丢弃。

您提到这是在 Windows 服务中,因此有一些事情在这里发挥作用。当您停止 Windows 服务时,您将有大约 30 秒的时间从 ServiceBase 实现中的 OnStop 方法返回。如果您未在此时间内返回,Windows 会向用户报告它无法停止该服务。您应该做的是向 BackgroundWorker 发出信号,表明您需要它停止(使用 CancelAsync 机制),处置工作线程,然后退出。但是,由于您要停止服务,所以这并不重要,因为整个进程无论如何都会终止,包括其中运行的任何线程。

如果您按照您所说的操作并等待工作线程完成(在 OnStop 方法中),您的服务可能会在用户看来好像已挂起,因为 Windows 会说它无法停止它,并且该进程仍将运行。

Since BackgroundWorker implements IDisposable, you should be disposing it when you are finished with it.

You mention that this is in a Windows service so there are a few things that come into play here. When you stop a Windows service, you get around 30 seconds to return from the OnStop method in your ServiceBase implementation. If you do not return within this time, Windows reports to the user that it could not stop the service. What you should do is signal to the BackgroundWorker that you need it to stop (using the CancelAsync mechanism), dispose of the worker, and then exit. However, since you are stopping the service, it doesn't really matter as the whole process will be terminated anyway, including any threads that are running within it.

If you were to do as you say and wait for the worker to complete (in the OnStop method), your service may appear to the user as if it has hung as Windows will say that it cannot stop it, and the process will still be running.

蓝眼睛不忧郁 2024-09-10 19:46:46

正确的方法是让工作线程干净利索地完成它正在做的事情。

这通常可以通过使用共享标志 \waithandle\whatever 来完成,该标志可用于与工作线程(从主线程)通信,表明它应该退出正在执行的操作,这自然会导致工作线程洗牌堆积并蒸发。工作线程应该定期检查“标志”以查看是否需要退出(例如在循环中),并且主服务线程可以在需要关闭时发出该标志信号。然后,主线程等待工作线程退出,例如通过在工作线程上调用 join 。

The correct method is to let the worker thread cleanly finish what it is doing.

This can often be done by having a shared flag\waithandle\whatever that can be used to communicate to the worker thread (from the main thread) that it should exit what it is doing, which will naturally lead to the worker thread shuffling off its stack and evaporating. The worker thread should regularly check the "flag" to see if it it needs to exit (e.g. in your loop), and the main service thread can signal the flag when it needs to shut down. The main thread then waits for the worker thread to exit e.g. by calling join on the worker thread.

甜宝宝 2024-09-10 19:46:46

BackgroundWorker 是 IDisposable,因此您应该在不再需要时将其丢弃。

BackgroundWorkers 还有一个 CancelAsync 方法,可将 CancellationPending 属性更改为 true。您可以在 DoWork 函数中检查这一点,以便阻止它运行。

BackgroundWorker is IDisposable, so you should dispose of it when you no longer need to.

BackgroundWorkers also have a CancelAsync method that changes the CancellationPending property to true. You can check for that in your DoWork function so you can stop it from running.

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