Pages

Thursday, July 11, 2013

Simple task (thread) implementation in C# .NET 4.0 and 4.5

This code implements a simple way to start a method in a thread and wait for it to end to get the return value of the method.



System.Threading.Tasks.Task<string> task =
 System.Threading.Tasks.Task.Factory.StartNew(() => myMethod(myString));

if (task.Status == System.Threading.Tasks.TaskStatus.RanToCompletion)
{
 //is done, do something!
 //use result task.Result
}

No comments:

Post a Comment