473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to terminate a blocked thread

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
}
}
Nov 15 '05 #1
4 4896
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()

"Dr. J" <no****@nowhere.com> wrote in message
news:mTTtb.20054$Dw6.92543@attbi_s02...
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
}
}

Nov 15 '05 #2
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.

Dr. J wrote:
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
}
}

Nov 15 '05 #3

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" <no****@nowhere.com>
| From: "Dr. J" <no****@nowhere.com>
| 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: ab***@comcast.net
| 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!TK2MSFTN GP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!newsfeed.icl.net!newsfeed.fjserv.net!log bridge.uor
egon.edu!arclight.uoregon.edu!wn13feed!wn12feed!wo rldnet.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
| }
| }
|
|
|

Nov 15 '05 #4
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

"Dr. J" <no****@nowhere.com> wrote in message news:<mTTtb.20054$Dw6.92543@attbi_s02>...
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
}
}

Nov 15 '05 #5

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

Similar topics

11
by: Bob Rock | last post by:
Hello, coming from win32 API I recall an ExitThread() call to gently terminate a thread from inside the same thread .... but now all I can see is an Abort call which seems to me a wrapper on the...
9
by: liangbowen | last post by:
As i konw, the only way to to terminate a thread started by _beginthread() is to user _endthread() (or return) inside the thread. now i started a thread(TodoThread), and created a listenning...
6
by: Iain | last post by:
I've got a (VC++ 6.0) com object which does something asynchronously (writes a DVD image, actually). It rasies COM events to indicate progress, including completion. I've got this running in a...
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: ...
3
by: Boniek | last post by:
Hi My main thread is waiting for data from database ( opened Sqlconnection etc) and my Main Form is also blocked. I have a ProgressBar on Status Bar which show to a user how percent data is...
1
by: [Yosi] | last post by:
How to terminate immediately ? after mThread.Start(); ... I want to kill the thread , but mThread.Abort() don't terminate it immediately, still see that the process keep going. Is there any way...
9
by: Li Pang | last post by:
Hi I make an app which can run some sub processes through multiple threads. I'd like to know how to terminate all sub-threads when the main thread is closed thanks in advance
3
by: Mr Dyl | last post by:
I'm using Boost to run a couple of threads, one of which reads commands via std::cin and another receives them through a socket. It's entirely possible that during the life of the app, all...
2
by: R. Nachtsturm | last post by:
Hi, i have the problem that when i create a low priority background thread, start it, and wait for it to finish that it does not seem to terminate even after it is finished.. if i use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...

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.