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
| }
| }
|
|
|