C # Background threads and foreground threads

Source: Internet
Author: User

The biggest difference between a background thread and a previous thread is that the background thread does not prevent the process from aborting, and when all foreground threads belonging to a process are aborted, the common language runtime ends the process, and all remaining background threads stop and do not complete.

The following is the demo code

using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ConsoleApplication1 {class Program {private static void Fntestthread (object data) {string sMsg; string sthreadname = Th Read. Currentthread.name; int nloopcount = (int) data; for (int i = 0; i < Nloopcount i++) {sMsg = string. Format ("Thread {0} Loop {1}", Sthreadname, I); Console.WriteLine (SMSG); Thread.Sleep (250); } SMSG = string. Format ("Thread {0} end", Sthreadname); Console.WriteLine (SMSG); static void Main (string[] args) {//foreground thread, performing 10 thread objforthread = new Thread (New Parameterizedthreadstart (Fntestthrea d)); Objforthread.name = "Foregroundthread"; Objforthread.start (20); Background thread, performing 50 thread objbackthread = new Thread (new Parameterizedthreadstart (Fntestthread)); Objbackthread.name = "Backgroundthread"; Objbackthread.isbackground = true; Objbackthread.start (50); } } }

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.