How to terminate a blocked thread

D

Dr. J

How to terminate a blocked thread?

In my form's "load" I launch a TCP listening thread that stays in an
infinite loop waiting for incoming TCP packets. In this form's "closing" I
try to terminate this thread by calling the "Abort" funcion. But the thread
does not terminate and after the form is closed this thread keeps running at
blocked state. Basically the application keeps running because this thread
does not terminate while blocked. The task manager shows the application
running even after closing of the form.

My question is what is proper method of shutting down blocked threads in an
application? Below is my code snippet to illustrate this situation.

Thanks in advance,

----------------------------------------------
// thread to listen synchronously in an infinite loop
public void ListenThread()
{
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener tcpl = new TcpListener(ipAddress, 49152);
tcpl.Start();

while(true)
{
Socket newSocket = tcpl.AcceptSocket();
// Blocked here
if(newSocket.Connected)
{
// do work here
newSocket.Close();
}
}
}



// launch the thread on "load"
Thread TcpListen;
private void Form1_Load(object sender, System.EventArgs e)
{
TcpListen = new Thread(new ThreadStart(ListenThread));
TcpListen.Start();
}



// try to terminate the thread in "closing"
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(TcpListen.IsAlive)
{
TcpListen.Abort (); // won't terminate the thread
}
}
 
M

mia lanui

You could try setting the IsBackground property of the thread in question to
true; this works for me given your test code.

Set this right after calling new Thread()
 
G

Greg Merideth

Worker threads run until the method that ThreadStart is passed
terminates or is "stopped by other means". I've always written threads
so that the ThreadStart method never has a while(true) event. Try
adding a "while(true&&!_threadstop)" then use a method in the thread to
signal the while loop to exit. Once the method ThreadStart exits, the
thread is killed and garbage collection can do its job freeing things up.
 
J

Jeffrey Tan[MSFT]

Hi,

I think beside you can follow Greg's suggestion to set a check variable,
you also can make check ManualResetEvent variable in your loop.
It provides you a way of notification.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Dr. J" <[email protected]>
| From: "Dr. J" <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: How to terminate a blocked thread
| Lines: 58
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <mTTtb.20054$Dw6.92543@attbi_s02>
| NNTP-Posting-Host: 24.7.10.145
| X-Complaints-To: (e-mail address removed)
| X-Trace: attbi_s02 1069025874 24.7.10.145 (Sun, 16 Nov 2003 23:37:54 GMT)
| NNTP-Posting-Date: Sun, 16 Nov 2003 23:37:54 GMT
| Organization: Comcast Online
| Date: Sun, 16 Nov 2003 23:37:54 GMT
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!newsfeed.icl.net!newsfeed.fjserv.net!logbridge.uor
egon.edu!arclight.uoregon.edu!wn13feed!wn12feed!worldnet.att.net!204.127.198
.203!attbi_feed3!attbi.com!attbi_s02.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:199724
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| How to terminate a blocked thread?
|
| In my form's "load" I launch a TCP listening thread that stays in an
| infinite loop waiting for incoming TCP packets. In this form's "closing"
I
| try to terminate this thread by calling the "Abort" funcion. But the
thread
| does not terminate and after the form is closed this thread keeps running
at
| blocked state. Basically the application keeps running because this
thread
| does not terminate while blocked. The task manager shows the application
| running even after closing of the form.
|
| My question is what is proper method of shutting down blocked threads in
an
| application? Below is my code snippet to illustrate this situation.
|
| Thanks in advance,
|
| ----------------------------------------------
| // thread to listen synchronously in an infinite loop
| public void ListenThread()
| {
| IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
| TcpListener tcpl = new TcpListener(ipAddress, 49152);
| tcpl.Start();
|
| while(true)
| {
| Socket newSocket = tcpl.AcceptSocket();
| // Blocked here
| if(newSocket.Connected)
| {
| // do work here
| newSocket.Close();
| }
| }
| }
|
|
|
| // launch the thread on "load"
| Thread TcpListen;
| private void Form1_Load(object sender, System.EventArgs e)
| {
| TcpListen = new Thread(new ThreadStart(ListenThread));
| TcpListen.Start();
| }
|
|
|
| // try to terminate the thread in "closing"
| private void Form1_Closing(object sender,
| System.ComponentModel.CancelEventArgs e)
| {
| if(TcpListen.IsAlive)
| {
| TcpListen.Abort (); // won't terminate the thread
| }
| }
|
|
|
 
N

N.K

Consider using Tcplistener.Pending() method so that AcceptSocket()
doesnt blocks the thread.

i.e , Slightly modify ListenThread() method like this :

if (tcpl.Pending()) // Non blocking
{
Socket newSocket = tcpl.AcceptSocket();
if(newSocket.Connected)
{
// do work here
newSocket.Close();
}

}

I guess this is what you are looking for ....

Regards,

Nirmal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top