Joe HM wrote:
Hello -
I have a function that calls Thread.Abort() to stop a thread in a
_Closed() Method of a GUI. The thread contains a blocking call on a
TCP socket and that is the easiest way to stop that.
Except that Thread.Abort will not stop a thread in a blocking socket
call - at least not right away. The socket calls end up in unmanaged
code, and so the request will not be acknowledged until the thread
returns to managed code (see the docs for thread.abort for further
information).
The best way to stop a socket thread, is to close the socket and then
exit the thread (this avoids the call to Thread.Abort).
This thread is also outputting strings in a RichTextBox and in some
rare instances I get a System.NullReferenceException when I exit the
GUI. It seems like the _Closed() Method calls Thread.Abort() and then
continues closing down/disposing the form elements while the thread is
still trying to access the RichTextBox.
I guess I might have to add some mutex or similar locking mechanism to
make sure that this cannot happen. I just throught that the
Thread.Abort() would wait ... or doesn't it?
It certainly does sound like a threading situation - but I hope you
aren't directly accessing the RichTextBox from the background thread...
That is a big no, no - and will most likely lead you to very
unexpected results and crashes.
This is part 1 of a 3 part article that explains thread/ui interactions
and how to do it properly:
http://msdn.microsoft.com/library/de...ms06112002.asp
The code is in C#, but the concepts apply equally to VB.NET
--
Tom Shelton