I would recommend reading this article:
.Net Threading You Need To Know.[
^]
If you use a newer .NET version, you can also use Tasks, I think
Task.ContinueWith()
and
Task.IsCompleted
might be of interest to you.
Example:
Task taskA = Task.Run( () => Thread.Sleep(2000));
try {
taskA.Wait(1000);
bool completed = taskA.IsCompleted;
Console.WriteLine("Task A completed: {0}, Status: {1}",
completed, taskA.Status);
if (! completed)
Console.WriteLine("Timed out before task A completed.");
}
catch (AggregateException) {
Console.WriteLine("Exception in taskA.");
}
Warning: if you want to change controls in another Form from a thread or task you need to use
InvokeRequired
, or your code will not be thread safe !