Welcome to WindowsClient.net | Sign in | Join

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();
   }

Posted: Aug 21 2008, 09:20 AM by zuker | with 1 comment(s)
Filed under:

Comments

EsticsKninacy said:

Where?

tell me pls LaurienGirvenqdv683@live.com

# October 2, 2008 2:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter