Home All Groups Group Topic Archive Search About

Have thread end if not finished in X seconds

Author
8 Jul 2009 6:13 PM
cj2
How can I force a thread to end withing X seconds if it is not done?

I think this will have to be done by the thread itself.  The main
program starts threads to process incoming requests and the thread
handles all processing then ends.  Currently the main program doesn't
monitor them at all.

Author
9 Jul 2009 10:36 AM
Colbert Zhou [MSFT]
Hi,

We can use the WaitHandle.WaitOne overload that takes a timespan parameter
indicating the number of milliseconds to wait for completion as a
simplified thread / method execution timeout mechanism. Here is a nice post
with detailed codes on this topic,

How to set a maximum execution time on a thread,
http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=1161

Hope this helps!

Best regards,
Colbert Zhou
Are all your drivers up to date? click for free checkup

Author
10 Jul 2009 1:47 AM
cj2
I must admit I don't understand all of the example but I think I get the
just of it.  If I'm understanding correctly the main procedure will have
to keep checking to see if the thread it launched has been executing to
long and if so end it.  Am I right?  I'm not sure that's what I want but
I'm still going over what I need.

I do have a couple related questions.

I'm wondering if the thread needs to do anything special when it's time
to terminate.  The sub that is the thread just ends--I was told there is
nothing special I need to do.  I'm wondering if the system is having a
hard time cleaning up finished threads or something.

Second question if when I shut down the main procedure I want it to
immediately stop all threads it has launched is there a way I can make
do this without having to keep track of each one individually?  Like
pulling a list of all running threads by the name of the program and
ending each.


Colbert Zhou [MSFT] wrote:
Show quoteHide quote
> Hi,
>
> We can use the WaitHandle.WaitOne overload that takes a timespan parameter
> indicating the number of milliseconds to wait for completion as a
> simplified thread / method execution timeout mechanism. Here is a nice post
> with detailed codes on this topic,
>
> How to set a maximum execution time on a thread,
> http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=1161
>
> Hope this helps!
>
> Best regards,
> Colbert Zhou
>

Bookmark and Share