Skip to content

Instantly share code, notes, and snippets.

@bhameyie
Last active December 18, 2015 16:59
Show Gist options
  • Save bhameyie/5815872 to your computer and use it in GitHub Desktop.
Save bhameyie/5815872 to your computer and use it in GitHub Desktop.
Simple example on how to spawn and wait for a new thread
var resetEvent = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(
arg =>
{
var task = new MyTaskRunner();
task.Run(data);
resetEvent.Set();
});
/*
The code below specifies a 1 minute timeout period.
If the resetEvent WaitOne method is true, that will indicate the task completed successfully
If the resetEvent WaitOne method is fales, that will indicate the task failed to complete (i.e. it timed-out)
*/
if(!resetEvent.WaitOne(new TimeSpan(0, 0, 1, 0))){
Console.WriteLine("The task timedout");
}
public class TaskRunner
{
public void Run(Dictionary<string, object> data)
{
try {
//Do whatever
}
catch (System.Exception ex) {
//Do whatever
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment