-
-
Save GraemeBradbury/e65f0f1cf283bf7b0367 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace AsyncPlayground | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var asyncCls = new ASyncClass(); | |
var tasks = new List<Task>(); | |
for (int i = 0; i < 10; i++) | |
{ | |
tasks.Add(asyncCls.MainTask(i)); | |
} | |
Task.WhenAll(tasks).Wait(); | |
var tasks2 = Enumerable.Range(0, 10).Select(asyncCls.MainTask); | |
Task.WhenAll(tasks2); | |
Console.ReadKey(); | |
} | |
} | |
public class ASyncClass | |
{ | |
public async Task MainTask(int i) | |
{ | |
Console.WriteLine("Main Task" + i); | |
if (i % 2 == 0) | |
{ | |
Console.WriteLine("Awaiting Subtask one" + i); | |
await SubTaskOne(i); | |
} | |
else | |
{ | |
Console.WriteLine("Awaiting Subtask two" + i); | |
await SubTaskTwo(i); | |
} | |
Console.WriteLine("Main Task completed" + i); | |
} | |
public async Task SubTaskOne(int i) | |
{ | |
Console.WriteLine("SubTask One"+i); | |
await Task.Delay(2000); | |
Console.WriteLine("SubTask One complete"+i); | |
} | |
public async Task SubTaskTwo(int i) | |
{ | |
Console.WriteLine("SubTask Two"+i); | |
await Task.Delay(5000); | |
Console.WriteLine("SubTask Two complete"+i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment