Thread timeout?

  • Thread starter Thread starter ll
  • Start date Start date
Timeout for the thread.
eg: assign 1 minutes to the thread, the thread could running for 2 minutes.
So when the timeout happen, the thread should terminate itself.
Can I do that? Thanks.
 
Althought its specific to your software, you should
probably manage this from within the thread. Terminating
threads from outside can lead to leaked resources - even
in .Net where, for example you may have native handles
that require proper cleanup, or calls to make to IDispose
within .Net.
Perhaps you would set a time-to-live (TTL) value for your
thread before invoking it, and have the thread check and
dispose/cleanup gracefully? I would not recommend that
you rely on Timers for this.
Of course the thread cannot guarantee to be millisecond
precise, but at least you can set limits.

HTH,

mk
 
Windows threads don't have this feature.
You have to implement this in the code that the thread executes.

B\rgds
100
 
In some other thread (OThread) that knows about the thread (S_Thread) you
want to time out, you could, in OThread have the following C# code:
if (!S_Thread.Join(timeout)) //suspend this thread
(OThread) until S_Thread finishes, or timeout is
//reached
S_Thread.Abort(); //S_Thread did not finish
during the timeout, so abort it.


or something similar.
 
you also need to catch for the abortexception inside the calling thread as
well for things to go smoothely. also, a thread is not guaranteed to honor
the threadabort exception in some cases that might be troublesome. after you
call abort in the calling thread you should then call join to wait on the
thread to abort.
 
Back
Top