Click here to Skip to main content
13,409,655 members (37,559 online)
Rate this:
Please Sign up or sign in to vote.
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)
        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)
    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
Updated 24-Dec-12 2:32am

1 solution

Rate this: bad
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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180221.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2018
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