Simplest Possible Background Thread and Console App Lifetime Test

/ Published in: C#
Save to your folder(s)

Saw some strange console app behavior while hosting had to doublecheck that a console app will not exit while background threads are running. In the example below, once the main method ends, the app stays alive for 10 seconds while the background thread finishes.

Copy this code and paste it in your HTML
  1. static void Main(string[] args)
  2. {
  3. Thread t = new Thread(SleepyTime);
  4. t.Start();
  6. Console.ReadKey();
  7. }
  9. static void SleepyTime()
  10. {
  11. Thread.Sleep(10000);
  12. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.