Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2003

Tip: Looking for answers? Try searching our database.

How to shut down blocking thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Barry Anderberg - 16 Dec 2003 21:43 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
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.