DCSIMG
Execute code within timeout using threads - Zuker On Foundations

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)
Execute code within timeout using threads

Via this post

Following is a quick way to execute code and limiting it within a certain timeout.

Note that the code spawns a new thread.
A better safer code would be using the ThreadPool and wait on the WaitHandle.

Thread t = new Thread(
   () =>
   {
      //Do Stuff Here
   });

   t.Start();
 
   bool success = t.Join(TimeSpan.FromSeconds(10));
   if (success)
   {
      //Thread completed successfully
   }
   else
   {
      throw new TimeoutException();
   }

Published Thursday, January 15, 2009 3:25 PM by Amir Zuker

תגים:,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: