Mega Code Archive

 
Categories / C# Book / 10 Thread
 

0635 Foreground and Background Threads

By default, threads you create are foreground threads. Foreground threads keep the application alive for as long as any one of them is running. Once all foreground threads finish, the application ends, and any background threads still running abruptly terminate. You can query or change a thread's background status using its IsBackground prop- erty. Here's an example: using System; using System.Threading; class Program { static void Main(string[] args) { Thread worker = new Thread(() => Console.ReadLine()); if (args.Length > 0) worker.IsBackground = true; worker.Start(); } }