C # foreground and background threads,
The process will wait for all foreground threads to finish the work, but if only the background threads are left, the work will end directly.
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. threading; using System. diagnostics; namespace testThread {class Program {static void Main (string [] args) {var sampleForegroud = new ThreadSample (10); var sampleBackgroud = new ThreadSample (20 ); var threadone = new Thread (sampleForegroud. countNumbers); threadone. name = "ForegroudThread"; var threadtwo = new Thread (sampleBackgroud. countNumbers); threadtwo. name = "BackgroudThread"; threadtwo. isBackground = true; threadone. start (); threadtwo. start () ;}} class ThreadSample {private readonly int _ iterations; public ThreadSample (int iterations) {_ iterations = iterations;} public void CountNumbers () {for (int I = 0; I <_ iterations; I ++) {Thread. sleep (TimeSpan. fromSeconds (0.5); Console. writeLine ("{0} Print {1}", Thread. currentThread. name, I) ;}}} View Code