C # multithreading -- foreground thread and background thread

Source: Internet
Author: User

Due to the time slice, although all threads are executed in a serial manner at the micro level, they can be considered as parallel execution at the macro level.

There are two types of threads: foreground and background. We can use the thread attribute isbackground = false to specify the frontend and backend attributes of the thread (the default is the foreground thread ).

The difference is that the program of the foreground thread can exit only after all the foreground threads have finished running. The program of the background thread, as long as the foreground thread is terminated, then the background thread will automatically end and launch the program.

Usage direction: Generally, foreground threads are used for long-waiting tasks, such as listening to client requests. Background threads are generally used to process short-time tasks, such as processing client-sent request information.

Foreground thread]

using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace Demo{    class Program    {        static void Main(string[] args)        {            Thread aThread = new Thread(threadFunction);            Console.WriteLine("Thread is starting...");            aThread.Start();            Console.WriteLine("Application is terminating...");        }        public static void threadFunction()        {            Console.WriteLine("Thread is sleeping...");            Thread.Sleep(5000);            Console.WriteLine("Thread is aborted!");        }    }}

Background thread]

using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace Demo{    class Program    {        static void Main(string[] args)        {            Thread aThread = new Thread(threadFunction);            aThread.IsBackground = true;            Console.WriteLine("Thread is starting...");            aThread.Start();            Console.WriteLine("Application is terminating...");        }        public static void threadFunction()        {            Console.WriteLine("Thread is sleeping...");            Thread.Sleep(5000);            Console.WriteLine("Thread is aborted!");        }    }}

 

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.