Answers for "c# run multiple tasks in parallel"

C#
0

c# run multiple tasks in parallel

1 using System;
 2 using System.Threading;
 3 using System.Threading.Tasks;
 4 
 5 class Program
 6 {
 7     static void Main(string[] args)
 8     {
 9         Task<int[]> parent = new Task<int[]>(() =>
10         {
11             var results = new int[3];
12             new Task(() => {
13                 Thread.Sleep(15000);
14                 results[0] = 0;
15             },
16             TaskCreationOptions.AttachedToParent).Start();
17             new Task(() => results[1] = 1,
18             TaskCreationOptions.AttachedToParent).Start();
19             new Task(() => results[2] = 2,
20             TaskCreationOptions.AttachedToParent).Start();
21             return results;
22         });
23         parent.Start();
24         var finalTask = parent.ContinueWith(
25         parentTask => {
26             foreach (int i in parentTask.Result)
27                 Console.WriteLine(i);
28         });
29         finalTask.Wait();
30         Console.ReadLine();
31     }
32 }
Posted by: Guest on October-23-2020

C# Answers by Framework

Browse Popular Code Answers by Language