473,857 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("lo calhost").Addre ssList[0];
TcpListener tcpl = new TcpListener(ipA ddress, 49152);
tcpl.Start();

while(true)
{
Socket newSocket = tcpl.AcceptSock et();
// Blocked here
if(newSocket.Co nnected)
{
// do work here
newSocket.Close ();
}
}
}

// launch the thread on "load"
Thread TcpListen;
private void Form1_Load(obje ct sender, System.EventArg s e)
{
TcpListen = new Thread(new ThreadStart(Lis tenThread));
TcpListen.Start ();
}

// try to terminate the thread in "closing"
private void Form1_Closing(o bject sender,
System.Componen tModel.CancelEv entArgs e)
{
if(TcpListen.Is Alive)
{
TcpListen.Abort (); // won't terminate the thread
}
}
Nov 15 '05 #1
4 4943
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.2005 4$Dw6.92543@att bi_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("lo calhost").Addre ssList[0];
TcpListener tcpl = new TcpListener(ipA ddress, 49152);
tcpl.Start();

while(true)
{
Socket newSocket = tcpl.AcceptSock et();
// Blocked here
if(newSocket.Co nnected)
{
// do work here
newSocket.Close ();
}
}
}

// launch the thread on "load"
Thread TcpListen;
private void Form1_Load(obje ct sender, System.EventArg s e)
{
TcpListen = new Thread(new ThreadStart(Lis tenThread));
TcpListen.Start ();
}

// try to terminate the thread in "closing"
private void Form1_Closing(o bject sender,
System.Componen tModel.CancelEv entArgs e)
{
if(TcpListen.Is Alive)
{
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("lo calhost").Addre ssList[0];
TcpListener tcpl = new TcpListener(ipA ddress, 49152);
tcpl.Start();

while(true)
{
Socket newSocket = tcpl.AcceptSock et();
// Blocked here
if(newSocket.Co nnected)
{
// do work here
newSocket.Close ();
}
}
}

// launch the thread on "load"
Thread TcpListen;
private void Form1_Load(obje ct sender, System.EventArg s e)
{
TcpListen = new Thread(new ThreadStart(Lis tenThread));
TcpListen.Start ();
}

// try to terminate the thread in "closing"
private void Form1_Closing(o bject sender,
System.Componen tModel.CancelEv entArgs e)
{
if(TcpListen.Is Alive)
{
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 ManualResetEven t 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.publi c.dotnet.langua ges.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$Dw 6.92543@attbi_s 02>
| NNTP-Posting-Host: 24.7.10.145
| X-Complaints-To: ab***@comcast.n et
| 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.ph x.gbl!cpmsftngx a09.phx.gbl!TK2 MSFTNGP08.phx.g bl!newsfeed00.s u
l.t-online.de!t-online.de!newsf eed.icl.net!new sfeed.fjserv.ne t!logbridge.uor
egon.edu!arclig ht.uoregon.edu! wn13feed!wn12fe ed!worldnet.att .net!204.127.19 8
.203!attbi_feed 3!attbi.com!att bi_s02.POSTED!n ot-for-mail
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1997 24
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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("lo calhost").Addre ssList[0];
| TcpListener tcpl = new TcpListener(ipA ddress, 49152);
| tcpl.Start();
|
| while(true)
| {
| Socket newSocket = tcpl.AcceptSock et();
| // Blocked here
| if(newSocket.Co nnected)
| {
| // do work here
| newSocket.Close ();
| }
| }
| }
|
|
|
| // launch the thread on "load"
| Thread TcpListen;
| private void Form1_Load(obje ct sender, System.EventArg s e)
| {
| TcpListen = new Thread(new ThreadStart(Lis tenThread));
| TcpListen.Start ();
| }
|
|
|
| // try to terminate the thread in "closing"
| private void Form1_Closing(o bject sender,
| System.Componen tModel.CancelEv entArgs e)
| {
| if(TcpListen.Is Alive)
| {
| TcpListen.Abort (); // won't terminate the thread
| }
| }
|
|
|

Nov 15 '05 #4
N.K
Consider using Tcplistener.Pen ding() method so that AcceptSocket()
doesnt blocks the thread.

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

if (tcpl.Pending() ) // Non blocking
{
Socket newSocket = tcpl.AcceptSock et();
if(newSocket.Co nnected)
{
// 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.200 54$Dw6.92543@at tbi_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("lo calhost").Addre ssList[0];
TcpListener tcpl = new TcpListener(ipA ddress, 49152);
tcpl.Start();

while(true)
{
Socket newSocket = tcpl.AcceptSock et();
// Blocked here
if(newSocket.Co nnected)
{
// do work here
newSocket.Close ();
}
}
}

// launch the thread on "load"
Thread TcpListen;
private void Form1_Load(obje ct sender, System.EventArg s e)
{
TcpListen = new Thread(new ThreadStart(Lis tenThread));
TcpListen.Start ();
}

// try to terminate the thread in "closing"
private void Form1_Closing(o bject sender,
System.Componen tModel.CancelEv entArgs e)
{
if(TcpListen.Is Alive)
{
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
2225
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 TerminateThread() Win32 API which is a brutal way to end a thread. Is there any other more gentle way to close a thread??? Bob Rock
9
13070
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 socket inside the thread. I want to destroy the socket and kill the thread at anytime if there is no client connect to it. How?
6
3094
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 c# application, with the events coming up and being caught (The idea is to use this to Set a ManualResetEvent which will alow the rest of the processign to happen). However, when I put an event after the call to start the async process off,
6
4133
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
3
3590
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 loaded. I use ThreadTimer and Thread but I can't do anything because when a thread (or ThreadTimer) go to the line ProgressBar.PefromStep() then it stop on that line. I think it's work like that because the Main Thread (and main Form) is sleeping....
1
1966
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 to terminate it immediately ?
9
7423
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
5981
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 commands will be sent through the socket and the thread watching cin will never "see any action". So the problem is, how do I terminate this thread that waits for keyboard input? Unfortunately, Boost::thread doesn't yet allow me to force the thread...
2
1730
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 performance monitoring to watch the actual thread count then it never goes down.. everytime i create a thread it goes up by one.. even if i abort the thread and set the thread object = nothing it still doesn't decrease
0
9768
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
11083
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
10711
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
10808
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,...
1
7940
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5774
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
5971
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
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.