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
Barry
Rick Strahl [MVP] - 16 Dec 2003 23:11 GMT
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 ---

Signature
Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/blog/
----------------------------------
Making waves on the Web
> 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
[quoted text clipped - 10 lines]
>
> Barry
Peter Koen - 16 Dec 2003 23:13 GMT
> 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
[quoted text clipped - 10 lines]
>
> Barry
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

Signature
------ooo---OOO---ooo------
Peter Koen - www.kema.at
MCAD MCDBA
CAI/RS CASE/RS IAT
------ooo---OOO---ooo------
Miha Markic - 18 Dec 2003 13:38 GMT
Hi Barry,
Thread.Abort will work.
It doesn't work when thread is within unmanaged call.

Signature
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
> 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
[quoted text clipped - 10 lines]
>
> Barry
Socket - 27 Dec 2003 11:21 GMT
.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
Thread.Abort will as you say not work, since the socket read call is unmanaged.