Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 .NET4 Task
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 });
How important in such case to invoke Dispose ???
Posted 17-Dec-12 5:19am
Edited 24-Dec-12 2:32am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Oleksandr Kulchytskyi at 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)

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 330
2 Richard MacCutchan 225
3 BillWoodruff 185
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100