472,354 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

Is Thread.Abort() blocking until Thread has exited?

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.

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?

Any suggestions?

Thanks!
Joe

Sep 1 '06 #1
6 5341

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

Sep 1 '06 #2
Joe I had the same problem with a UDP socket in a thread...i had to do this
and it worked.

'Without this the thread could still remain open
receivingUdpClient.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.Abort()

Miro

"Tom Shelton" <to*@mtogden.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
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

Sep 1 '06 #3
Hello -

Thanks for the info!

I changed the code to not Abort() the Thread anymore but rather call
the Close() on the NetworkStream and then the Close() on the TCPClient.
This seems to be working without a problem.

The Thread actually calls a function outside the Thread that calls a
function in the Form to add the text to the RichTextBox. So that
should be fine ... I will still look at that article you referred to,
though ...

Thanks!
Joe

Tom Shelton wrote:
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
Sep 1 '06 #4

Joe HM wrote:
Hello -

Thanks for the info!

I changed the code to not Abort() the Thread anymore but rather call
the Close() on the NetworkStream and then the Close() on the TCPClient.
This seems to be working without a problem.
That's usually the easiest way.
The Thread actually calls a function outside the Thread that calls a
function in the Form to add the text to the RichTextBox. So that
should be fine ... I will still look at that article you referred to,
though ...
Well Joe, if the thread calls the function, then the function is
executed in the context of the thread. Unless you use the forms Invoke
method, then you are probably going to end up with issues. An easy way
to find out is to insert a test in the function that is updateing the
RTF box. Just test the controls InvokeRequired property. If it
returns true, and you aren't using the Invoke method to update the
control - then you have an issue.

--
Tom Shelton

Sep 1 '06 #5
Hello Tom -

Thanks for the info ... I will definitely give that a try and look into
the issue!

Thanks!
Joe

Tom Shelton wrote:
Joe HM wrote:
Hello -

Thanks for the info!

I changed the code to not Abort() the Thread anymore but rather call
the Close() on the NetworkStream and then the Close() on the TCPClient.
This seems to be working without a problem.

That's usually the easiest way.
The Thread actually calls a function outside the Thread that calls a
function in the Form to add the text to the RichTextBox. So that
should be fine ... I will still look at that article you referred to,
though ...

Well Joe, if the thread calls the function, then the function is
executed in the context of the thread. Unless you use the forms Invoke
method, then you are probably going to end up with issues. An easy way
to find out is to insert a test in the function that is updateing the
RTF box. Just test the controls InvokeRequired property. If it
returns true, and you aren't using the Invoke method to update the
control - then you have an issue.

--
Tom Shelton
Sep 1 '06 #6
Hello Tom -

Thanks for the info ... I will definitely give that a try and look into
the issue!

Thanks!
Joe

Tom Shelton wrote:
Joe HM wrote:
Hello -

Thanks for the info!

I changed the code to not Abort() the Thread anymore but rather call
the Close() on the NetworkStream and then the Close() on the TCPClient.
This seems to be working without a problem.

That's usually the easiest way.
The Thread actually calls a function outside the Thread that calls a
function in the Form to add the text to the RichTextBox. So that
should be fine ... I will still look at that article you referred to,
though ...

Well Joe, if the thread calls the function, then the function is
executed in the context of the thread. Unless you use the forms Invoke
method, then you are probably going to end up with issues. An easy way
to find out is to insert a test in the function that is updateing the
RTF box. Just test the controls InvokeRequired property. If it
returns true, and you aren't using the Invoke method to update the
control - then you have an issue.

--
Tom Shelton
Sep 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
2
by: Bruce Vander Werf | last post by:
How can I cleanly stop a thread that is currently blocking on Socket.Receive? I don't want to use Thread.Abort, because I would like the thread method to exit cleanly, and the same code must run...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
3
by: Raj Wall | last post by:
Hi, I have an application that uses a number of sub-threads. What is the best way to do some processing in each thread when the main application is shut down? Is the ThreadAbortException...
2
by: Christopher Carnahan | last post by:
I need to figure out how to terminate a thread while it is blocked trying to create a COM object via interop. In a worker thread, I do something like this: Type t = null; Object...
2
by: Miro | last post by:
VB 2003. I cant find the last thing im missing. I click the "Run" button to run my app in VB.net and it runs. But when i close the application, the thread does not end ( i think ) because it...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.