Last active
December 18, 2015 16:59
-
-
Save bhameyie/5815872 to your computer and use it in GitHub Desktop.
Simple example on how to spawn and wait for a new thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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