Setting the lifespan of a thread

  • Thread starter Thread starter Nicholas Beenham
  • Start date Start date
N

Nicholas Beenham

Hi all,

Is there a way to set the lifespan of a thread.

I am trying to start a process that may or may not have an outcome and want
to finish it after a certain length of time finished or not.

So I have

Thread t = new Thread(new ThreadStart(targetMethod));
t.start();

how can I terminate t after a length of time I decide?

Thanks
Nick
 
How about using Join(timeSpan). If you don't want to block the calling
thread, have it start a "joiner thread" that itself starts your worker
thread and then calls Join on it.

The "joiner thread" would call Abort on the worker thread when it gets
control, if the latter has not yet finished.
 
Hi,

Nicholas Beenham said:
So would I use a timer then call abort?

Yes, or the solution Fred suggested.
You might even spawn another thread which does the "dirty" job :)
 
Back
Top