Groups | Blog | Home
all groups > dotnet clr > december 2003 >

dotnet clr : How to shut down blocking thread


Rick Strahl [MVP]
12/16/2003 1:11:54 PM
Hi Barry,

The best way to do this is to build some check logic into your thread loop.
At the beginning of the loop have it check some condition

while(.t.)
{
if (this.Cancelled)
exit;

... do your thread processing
}

Then your main thread code can set this flag in some way and cause the
thread to exit.

As to your blocking Socket call there's no way to deal with that other than
to possibly use an Async Delegate and check the delegate for completion. The
advantgate there is that your thread loop can stay active and wait for
completion as well as also check for cancellation requests. But this means
you will use yet another thread to actually process the socket.


+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/blog/
----------------------------------
Making waves on the Web


[quoted text, click to view]

banderbe NO[at]SPAM yahoo.com
12/16/2003 1:43:31 PM
I have a thread that sits in an infinite loop making a blocking socket
listen call. When incoming connections arrive they're fed into the
program, and the thread continues listening.

The question is, what do I do if I want to kill my application? How
can I interrupt the socket from blocking and then abort the thread
itself?

I know Thread.Abort() but I do not think this will work if the thread
is blocking.

Thanks

Peter Koen
12/16/2003 3:13:46 PM
banderbe@yahoo.com (Barry Anderberg) wrote in
news:9dd925b8.0312161343.3f31bd3b@posting.google.com:

[quoted text, click to view]

Use a Flag to indicate to the thread that the app is about to exit and if
the flag is set simply return from the thread proc.

if you have a thread waiting inifite time for networkpackets or similar,
you should define it as a backgroundthread anyways.

Or even better, use a thread from the ThreadPool, those get always
destroyed when the last foreground thread of your app terminates.

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD MCDBA
CAI/RS CASE/RS IAT

Miha Markic
12/18/2003 2:38:51 PM
Hi Barry,

Thread.Abort will work.
It doesn't work when thread is within unmanaged call.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

[quoted text, click to view]

Socket
12/27/2003 3:21:05 AM
..Close() the socket from your main thread, that will cause a InvalidOperationException from the blocking Read-call on the socket. You need to catch that exception and hide it or do something appropriate for your application
AddThis Social Bookmark Button