Click here to Skip to main content
15,898,571 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to use message box in windows service project. Pin
Dan Neely14-May-08 9:57
Dan Neely14-May-08 9:57 
AnswerRe: how to use message box in windows service project. Pin
Member 9614-May-08 8:47
Member 9614-May-08 8:47 
GeneralRe: how to use message box in windows service project. Pin
prasadbuddhika14-May-08 21:04
prasadbuddhika14-May-08 21:04 
GeneralRe: how to use message box in windows service project. Pin
Member 9615-May-08 5:28
Member 9615-May-08 5:28 
Questionthread safe in Dispose method Pin
George_George14-May-08 2:57
George_George14-May-08 2:57 
AnswerRe: thread safe in Dispose method Pin
Christian Graus14-May-08 3:00
protectorChristian Graus14-May-08 3:00 
GeneralRe: thread safe in Dispose method Pin
George_George14-May-08 15:16
George_George14-May-08 15:16 
GeneralRe: thread safe in Dispose method Pin
Peter Josefsson Sweden14-May-08 16:08
Peter Josefsson Sweden14-May-08 16:08 
They may not. Strictly speaking, if two threads or objects hold references to a single object, neither of them may call Dispose.

If they do, they are violating the fundamental rules of the IDisposable pattern.

The rules in short:

1. Dispose is meant to tell an object this: "I now know for sure that nobody has a reference to you except for myself, and I will never talk to you again - you may now safely throw away any resources you've been holding on to. If I or someone else ever talks to you again, please throw an exception.".

2. If you don't know for sure that you're the only one holding a reference to the object, you should never call Dispose. Just drop your reference (as in myRef = null;). The GC (garbage collector) will then call its finalizer (~TheClass) as soon as a) it realizes that nobody holds a reference to the object any more and b) it or your program has nothing more important to do. The finalizer will call Dispose(false), ensuring that any unmanaged (operating system handles and whatnot) will finally be released.

The only reason that the docs say that you should always allow calls to Dispose even if you are already disposed or not completely initialized is that you may in some cases get called from a catch or finally block that shouldn't have to check whether or not the try block that should have called you actually did it before the exception got thrown. This can never occur simultaneously on different threads, though.

So, the IDisposable stuff is only an optimization, enabling us to force expensive resources to be released before the GC gets around to it - but it should never be used unless we know for sure that the object is dead for good. Note that there is a lot of code (even in the framework) that just crashes spectacularly instead of throwing the proper exceptions if you try to use an object that has been disposed. Try loading a bitmap, showing it in a picture box and then disposing it (while still showing it). Results will vary - the app may exit silently, throw a protection fault or even display random graphics. At least, this was true in 1.1 - it was a while since I tested that.

Later,

--
Peter

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 
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 

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.