在Task的实例中调用Dispose方法

问题描述:

大家好。

我来这里有一个与Task类中的Dispose方法相关的问题。

这个方法有多强制?每当我完成任务实例的工作时,我是否需要调用它。

我在Reflector的帮助下调查了Dispose方法,我发现在内部调用它时它会执行一些清理工作特别针对ManualResetEvent的东西。



Hi guys.
I come here with one question related to Dispose method in Task class.
How mandatory this method is ? And do i need to invoke it every time when i finished to work with task instance.
I have investigated Dispose method with help of Reflector, and i found that internally when we calling it it perform some clean up stuff especially against ManualResetEvent.

protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        if ((this.Options & 0x4000) != TaskCreationOptions.None)
        {
            return;
        }
        if (!this.IsCompleted)
        {
            throw new InvalidOperationException(Environment.GetResourceString("Task_Dispose_NotCompleted"));
        }
        ContingentProperties contingentProperties = this.m_contingentProperties;
        if (contingentProperties != null)
        {
            ManualResetEventSlim completionEvent = contingentProperties.m_completionEvent;
            if (completionEvent != null)
            {
                contingentProperties.m_completionEvent = null;
                if (!completionEvent.IsSet)
                {
                    completionEvent.Set();
                }
                completionEvent.Dispose();
            }
        }
    }
    this.m_stateFlags |= 0x40000;
}



主要关注的是下一个:

我常常使用这样的结构:


The main concern about that lies in next:
Quite often i use such construction:

Task.Factory.StartNew(()=>{ .. some stufff });



在这种情况下调用Dispose有多重要???


How important in such case to invoke Dispose ???

这取决于。



哪个可能没有很有帮助,但是......



你应该在显式实现它的任何对象上调用Dispose,因为这个想法是对象包含或使用的资源稀缺供应,或使用大量空间 - 是记忆或其他东西。如果没有调用Dispose(显式地,或者通过使用块将其封装在中),则在调用垃圾收集器之前不会释放资源并确定不再需要对象 - 此时它将在对象上调用Dispose。因为这只发生在内存开始不足的正常情况下,所以不会发生几个小时,几天或几周 - 或者直到应用程序终止。



如果你的对象没有实现IDisposable那么你就不需要明确地调用Dispose。



你的一些对象(绝不是全部) Dispose应该是SqlCommand,SqlConnection,所有图形对象,字体,画笔,位图等。
It depends.

Which may not be much help, but...

You should call Dispose on any object which explicitly implements it, because the idea is that the object contains or uses resources which are in scarce supply, or which use a lot of "space" - be that memory or something else. If Dispose isn''t called (either explicitly, or by enclosing it in a using block) then the resources are not released until the Garbage Collector is called in and decides that the object is no longer needed - at which point it will call Dispose on the object. Because this only happens in normal circumstances when memory starts to run low, it man not happen for several hours, or days, or weeks - or until the application terminates.

If your objects don''t implement IDisposable then you shouldn''t need to explicitly call Dispose.

Some of the objects (and by no means all) which you should Dispose are SqlCommand, SqlConnection, all Graphics objects, Fonts, Brushes, Bitmaps and so forth.