Skip to content

Instantly share code, notes, and snippets.

@magoo-magoo
Created December 1, 2019 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magoo-magoo/f45776de09552aadc16689b103631c05 to your computer and use it in GitHub Desktop.
Save magoo-magoo/f45776de09552aadc16689b103631c05 to your computer and use it in GitHub Desktop.
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