Skip to content

Instantly share code, notes, and snippets.

@negativeeddy
Created August 27, 2018 16:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save negativeeddy/63c1ee8506fb7c81e038180a9fc62465 to your computer and use it in GitHub Desktop.
code snippet for blog regarding Threading Abort and async/await
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncVsAbort
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Thread thread1 = new Thread(async () => await PrintEvenNumbers());
thread1.Start();
Thread thread2 = new Thread(async () => await PrintOddNumbers());
thread2.Start();
Thread.Sleep(1000);
thread1.Abort();
thread2.Abort();
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
static async Task PrintEvenNumbers()
{
for (int i = 0; i < 50; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
//Thread.Sleep(100);
await Task.Delay(100);
}
}
static async Task PrintOddNumbers()
{
for (int i = 0; i < 50; i++)
{
if (i % 2 != 0)
{
Console.WriteLine(i);
}
Thread.Sleep(100);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment