Click here to Skip to main content
15,911,531 members
Home / Discussions / C#
   

C#

 
GeneralRe: thread safe in Dispose method Pin
Peter Josefsson Sweden14-May-08 16:08
Peter Josefsson Sweden14-May-08 16:08 
GeneralRe: thread safe in Dispose method Pin
George_George14-May-08 16:20
George_George14-May-08 16:20 
GeneralRe: thread safe in Dispose method Pin
Peter Josefsson Sweden14-May-08 17:00
Peter Josefsson Sweden14-May-08 17:00 
GeneralRe: thread safe in Dispose method Pin
George_George14-May-08 17:26
George_George14-May-08 17:26 
GeneralRe: thread safe in Dispose method Pin
Peter Josefsson Sweden15-May-08 8:51
Peter Josefsson Sweden15-May-08 8:51 
GeneralRe: thread safe in Dispose method Pin
George_George16-May-08 20:42
George_George16-May-08 20:42 
GeneralRe: thread safe in Dispose method Pin
Peter Josefsson Sweden16-May-08 21:46
Peter Josefsson Sweden16-May-08 21:46 
AnswerRe: thread safe in Dispose method Pin
Peter Josefsson Sweden14-May-08 5:57
Peter Josefsson Sweden14-May-08 5:57 
Hi,

I've seen others have this concern and have seen a multitude of solutions, but like Christian says, why? I only realized this recently (and felt pretty stupid about that), but like he says, there is NO circumstance whatsoever where it is correct for two different threads to invoke Dispose() on the same object.

In fact, if Dispose() is ever invoked twice on the same object (even from the same thread), it is a design fault. It is explicitly wrong to invoke Dispose() on an object if there is a chance (no matter how slim) that someone else is holding a reference to it (meaning that if you ever directly or indirectly expose a reference to an object outside of your class, you should never dispose the object).

Further, Dispose() cannot be invoked unless the object is alive, so it can never conflict with the finalizer being invoked by the GC.

The most common pattern for implementing IDisposable that I've seen is:

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Clean up managed resources here - invoke Dispose() on privately held objects that
            // implement IDisposable, and null out other (expensive) object references so that the
            // GC can reclaim them even if the caller keeps its reference to us:
            // ...
        }
        // Clean up unmanaged (allocated via interop calls etc) resources here:
        // ...
        // If this is a derived class and the base class implements IDisposable as well:
        // base.Dispose(disposing);
        disposed = true;
    }
}

public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

~MyClass()
{
    Dispose(false);
}

Note: Written from memory, beware of spelling mistakes. Google for "IDisposable pattern" and you will get all the examples you need (good and bad).

Later,

--
Peter

GeneralRe: thread safe in Dispose method Pin
George_George14-May-08 15:21
George_George14-May-08 15:21 
QuestionDllImport question Pin
matt23lucier14-May-08 2:20
matt23lucier14-May-08 2:20 
AnswerRe: DllImport question Pin
Christian Graus14-May-08 2:38
protectorChristian Graus14-May-08 2:38 
AnswerRe: DllImport question Pin
Dario Solera14-May-08 2:46
Dario Solera14-May-08 2:46 
AnswerRe: DllImport question Pin
carbon_golem14-May-08 4:28
carbon_golem14-May-08 4:28 
AnswerRe: DllImport question Pin
matt23lucier16-May-08 8:16
matt23lucier16-May-08 8:16 
QuestionHow to include visio diagrams in C#.net application Pin
ysunil_7414-May-08 1:33
ysunil_7414-May-08 1:33 
AnswerRe: How to include visio diagrams in C#.net application Pin
Gareth H14-May-08 1:37
Gareth H14-May-08 1:37 
AnswerRe: How to include visio diagrams in C#.net application Pin
Simon P Stevens14-May-08 1:55
Simon P Stevens14-May-08 1:55 
QuestionHow can i obtain a textbox in my webbrowser and pass the text a windows form? Pin
solbyte14-May-08 1:19
solbyte14-May-08 1:19 
AnswerRe: How can i obtain a textbox in my webbrowser and pass the text a windows form? Pin
Christian Graus14-May-08 1:37
protectorChristian Graus14-May-08 1:37 
QuestionCrystal Report Pin
mehrdadc4814-May-08 0:40
mehrdadc4814-May-08 0:40 
AnswerRe: Crystal Report Pin
ChandraRam14-May-08 1:20
ChandraRam14-May-08 1:20 
QuestionOnPaintBackground Problem. Pin
hdv21214-May-08 0:21
hdv21214-May-08 0:21 
QuestionUploading picture to server and display it in an Image control Pin
Dextter14-May-08 0:18
Dextter14-May-08 0:18 
AnswerRe: Uploading picture to server and display it in an Image control Pin
Christian Graus14-May-08 0:24
protectorChristian Graus14-May-08 0:24 
QuestionHow to customize .Net Textbox control Pin
NarVish13-May-08 23:45
NarVish13-May-08 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.