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