I start some task like this
Task.Factory.StartNew(SomeMethod, TaskCreationOptions.LongRunning);
void SomeMethod()
{
}
Is there is a way to return data when task is completed?
like
List<User> SomeMethod()
{
List<User> = List<User>();
...
return userList;
}
Yes like this.
By using the generic overload of
Task.Factory.StartNew<TResult>(Func<TResult> function)your task defines a return type. Then withtask.Resultyou can access your result.If you would like to pass one or more parameters to your function use a lambda to start your function:
For more information about threading and Task Parallelism I would like to refer you to the online ebook: Threading in C# by Joseph Albahari