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 quartz.net 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();
  5.  
  6. Console.ReadKey();
  7. }
  8.  
  9. static void SleepyTime()
  10. {
  11. Thread.Sleep(10000);
  12. }

URL: http://www.albahari.com/threading/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.