C#:我需要处理在运行时创建一个BackgroundWorker? [英] C#: Do I need to dispose a BackgroundWorker created at runtime?

查看:389
本文介绍了C#:我需要处理在运行时创建一个BackgroundWorker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常有这样的代码形式:

I typically have code like this on a form:

    private void PerformLongRunningOperation()
    {
        BackgroundWorker worker = new BackgroundWorker();

        worker.DoWork += delegate
        {
            // perform long running operation here
        };

        worker.RunWorkerAsync();
    }

这意味着我不处理的的BackgroundWorker ,而如果我通过表单设计器添加它,然后我觉得它会得到处置。

This means that I don't dispose the BackgroundWorker, whereas if I had added it by the form designer then I think it would get disposed.

这会带来什么问题?它是更正确的声明模块级 _saveWorker ,然后从该窗体的Dispose方法调用的Dispose 就可以了?

Will this cause any problems? Is it more correct to declare a module-level _saveWorker, and then call Dispose on it from the form's dispose method?

推荐答案

是的,你应该处置后台工作的。

Yes, you should dispose of the background worker.

您可能会发现它更容易使用 ThreadPool.QueueUserWorkItem(...),它不需要任何清理之后。

You may find it easier to use ThreadPool.QueueUserWorkItem(...) which doesn't require any clean up afterwards.

这是为什么你应该总是调用Dispose()的其他详细信息:

Additional detail on why you should always call Dispose():

但如果你在BackgroundWorker的类看它实际上并没有做任何线程清理它的Dispose方法,它仍然是重要的调用,因为类对垃圾收集器的效果丢弃。

Although if you look in the BackgroundWorker class it doesn't actually do any thread clean up in it's dispose method, it is still important to call Dispose because of the effect the class has on the garbage collector.

中的类终结并不GCed立即生效。它们保持并添加到终结队列。终结器线程然后运行,(它遵循标准模式调用Dispose)。这意味着该对象将存活至GC代1和Gen 1的集合远比根0集合罕见的,所以你在内存中的对象棒更长的时间。

Classes with finalizers are not GCed immediately. They are kept and added to the finalizer queue. The finalizer thread then runs, (which following the standard pattern calls dispose). This means the object will survive into GC generation 1. And gen 1 collections are far rarer than gen 0 collections, so you object sticks around in memory for much longer.

如果但是你调用Dispose(),对象将不会被添加到终结队列,因此可以自由地进行垃圾回收。

If however you call Dispose(), the object will not be added to the finalization queue, so is free to be garbage collected.

这不是真正的大问题,但如果你正在创造他们中的很多到头来你会使用更多的内存。它应该被认为是很好的做法,总是调用Dispose具有Dispose方法的对象。

It's not really big problem, but if you are creating a lot of them you'll end up using more memory than necessary. It should really be considered good practise to always call dispose on objects that have a dispose method.

所以我想,一切的一切,这不是100%的硬性需求。您的应用程序不会爆炸(甚至泄漏内存),如果你不调用Dispose(),但在某些情况下,可能产生负面影响。背景工人被设计用于从作为一个WinForms组件,因此使用它的方式,如果你有不同的要求,并且不希望使用它作为一个WinForms组件,不使用它,使用正确的工具的工作,像线程池。

So I suppose, all in all, it's not a 100% hard and fast requirement. Your app won't explode (or even leak memory) if you don't call Dispose(), but in some circumstances it may have negative effects. The background worker was designed for use from as a WinForms component so use it that way, if you have different requirements and don't want to use it as a WinForms component, don't use it, use the correct tool for the job, like the ThreadPool.

这篇关于C#:我需要处理在运行时创建一个BackgroundWorker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆