Task.Run vs Task.WhenAll vs Parallel.Invoke vs others: Run tasks in parallel and Get result in C#

  • Thread starter Thread starter Pingpong689
  • Start date Start date
P

Pingpong689

Guest
Is the code in Option 1 below the best way to achieve in running tasks in parallel?

**Option 1**

var w = Task.Run(async () => await Work1());
var w2 = Task.Run(async () => await Work2());
Console.WriteLine("end: " + DateTime.Now.ToString("hh:mm:ss.fff"));
await w;
await w2;
Console.WriteLine("last: " + DateTime.Now.ToString("hh:mm:ss.fff") + " " + w.Result + " " + w2.Result);
Console.WriteLine("end");


**Result:**

end: 06:07:13.054
Work2 s 06:07:13.057
Work1 s 06:07:13.057
Work1 e 06:07:16.072
Work2 e 06:07:17.066
last: 06:07:17.066 1 2

**option 2:**
It swithes to other work upon await, ie. not really in parallel.

var tasks = new List<Task<int>>();
tasks.Add(Work1());
tasks.Add(Work2());
Console.WriteLine("end: " + DateTime.Now.ToString("hh:mm:ss.fff"));
await Task.WhenAll(tasks);
Console.WriteLine("end2: " + DateTime.Now.ToString("hh:mm:ss.fff"));
Console.WriteLine("last: " + DateTime.Now.ToString("hh:mm:ss.fff") + " " + tasks[0].Result + " " + tasks[1].Result);

**Result:**

Work1 s 06:11:42.565
Work2 s 06:11:44.571
Work1 e 06:11:45.579
end: 06:11:47.572
Work2 e 06:11:48.574
end2: 06:11:48.575
last: 06:11:48.579 1 2


Tasks

private static async Task<int> Work1()
{
Console.WriteLine("Work1 s " + DateTime.Now.ToString("hh:mm:ss.fff"));
Thread.Sleep(2000);
await Task.Delay(1000);
Console.WriteLine("Work1 e " + DateTime.Now.ToString("hh:mm:ss.fff"));
return 1;
}

private static async Task<int> Work2()
{
Console.WriteLine("Work2 s " + DateTime.Now.ToString("hh:mm:ss.fff"));
Thread.Sleep(3000);
await Task.Delay(1000);
Console.WriteLine("Work2 e " + DateTime.Now.ToString("hh:mm:ss.fff"));
return 2;
}

Parallel.Invoke() doesn't return result.

Continue reading...
 
Back
Top