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