Created
December 1, 2019 21:44
-
-
Save magoo-magoo/f45776de09552aadc16689b103631c05 to your computer and use it in GitHub Desktop.
This file contains 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.Threading; | |
using System.Threading.Tasks; | |
namespace ThreadAsyncTest | |
{ | |
class Program | |
{ | |
static async Task DoSomething(int taskId) | |
{ | |
await Task.Delay(0); | |
var counter = 0; | |
while (counter < 10) | |
{ | |
Console.WriteLine($"Task {taskId} - Thread id {Thread.CurrentThread.ManagedThreadId} - count {counter++}"); | |
Thread.Sleep(100); | |
} | |
Console.WriteLine($"Task {taskId} finished."); | |
} | |
static async Task Main(string[] args) | |
{ | |
Console.WriteLine($"Program starts - Thread id {Thread.CurrentThread.ManagedThreadId}"); | |
var task1 = DoSomething(1); | |
var task2 = DoSomething(2); | |
await Task.WhenAll(task1, task2); | |
Console.WriteLine($"Program ends - Thread id {Thread.CurrentThread.ManagedThreadId}"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment