Created
August 27, 2018 16:25
Star
You must be signed in to star a gist
code snippet for blog regarding Threading Abort and async/await
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 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