Skip to content

Instantly share code, notes, and snippets.

@fatihbahceci
Created March 8, 2020 20:35
Show Gist options
  • Save fatihbahceci/5fb9d1730a2cc87026f367c0718c002a to your computer and use it in GitHub Desktop.
Save fatihbahceci/5fb9d1730a2cc87026f367c0718c002a to your computer and use it in GitHub Desktop.
C# Thread.IsAlive test
//C# Interactive code. (Ctrl + E, Ctrl + E)
using System.Threading;
volatile bool stop = false;
Thread thread = new Thread(() => {
while (stop == false)
{
Thread.Sleep(500);
Console.WriteLine(DateTime.Now);
}
Console.WriteLine("Finished");
});
Task.Delay(5 * 1000).ContinueWith(x => { stop = true; });
thread.Start();
Thread.Sleep(100);
while (thread.IsAlive) {
Thread.Sleep(100);
Console.WriteLine("Still working");
}
Console.WriteLine("Heyooo!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment