Skip to content

Instantly share code, notes, and snippets.

@aliabidzaidi
Last active June 2, 2019 17:15
Show Gist options
  • Save aliabidzaidi/97f8860073b9b9673a3dea8aa181f49a to your computer and use it in GitHub Desktop.
Save aliabidzaidi/97f8860073b9b9673a3dea8aa181f49a to your computer and use it in GitHub Desktop.
Returning result from a task and passing state
int number = 10;
//Using task factory + Setting Task State + Returning Result
Task<int> T1 = Task.Factory.StartNew<int>(obj =>
{
int num = (int)obj;
return num * num;
}, number);
Console.WriteLine("T1 Task Result" + T1.Result);
//Return Result of a Task with anonymous method and passing state
Task<int> T2 = new Task<int>(obj =>
{
int num = (int)obj;
return num * num;
}, number);
T2.Start();
Console.WriteLine("T2 Task Result=" + T2.Result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment