473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.NullRefe renceException 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 5479

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.NullRefe renceException 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
receivingUdpCli ent.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.A bort()

Miro

"Tom Shelton" <to*@mtogden.co mwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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.NullRefe renceException 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.NullRefe renceException 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
7391
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 I suspend and resume whenver needed. In order to properly shut down the program every thread has to be aborted. So, I override OnClosing(CancelEventArgs e) in my main GUI program, and have it perform the following on the thread: if...
2
12680
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 under the Compact Framework, which does not support Abort. Will Socket.Close cause the Receive method to finish, or is there a better way?
6
4125
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: Connect Receive response Send Connect ACK
20
3032
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 delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
3
6177
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 thrown automatically for each thread? Or is there some other event or exception automatically thrown that the thread can "grab" as it is shut down?
2
3506
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 activatedObject = null; Legacy.IScheduled comObject = null; t = Type.GetTypeFromProgID(ProgID);
2
9246
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 never ends the debuger and i have to hit the "little square" to stop the app. If make a shortcut to the exe and run it that way, it stays in the Windows Task Manager and I have to kill the process. Has anyone else run into this / work around?
5
5080
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 = proc.Start("notepad.exe");
18
10245
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 with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.