C # foreground and background threads,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.