Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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:
C#
Task.Factory.StartNew(()=>{ .. some stufff });

How important in such case to invoke Dispose ???
Posted
Updated 24-Dec-12 1:32am
v3

1 solution

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.
 
Share this answer
 
Comments
Oleksandr Kulchytskyi 24-Dec-12 7:55am    
Thanks for your answer, but if be honest i really know that if an objcets implements IDisposable, i need manually call Dispose method, or wrap it in scope of using block.
As concerns my question, i surf a lot in internet and got some confusion when i saw, that all articles which was written by MS, after finished of using Task object, do not call dispose method against instance of a Task.... or even didnt wrap in in using block. (I assume that most of MS developers knew about IDisposable interfale and resource disposing :) )
Thats why i have been asked this question..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900