C # Summary of asynchronous calls and threads

Source: Internet
Author: User

Http://blog.163.com/prince.king_521/blog/static/106891204201172010749138? Latestblog

1. Basic concepts of foreground and background threads

The main difference between foreground threads and background threads is that the application must run all foreground threads before exiting. For background threads, the application can exit directly regardless of whether it has been run. All background threads automatically end when the application exits.

Note: The threads created through the Thread class are all foreground threads. All threads in the threadpool (which will be introduced later) are background threads.

The thread pool is enabled for asynchronous delegate calls. Therefore, the thread for asynchronous delegate execution is the background thread.


When using thread to create a foreground thread, you can use the isbackground attribute to determine whether the thread is a foreground thread or a background thread. See the following code:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace BackAndForThread
{
     class Program
    {
          static void Main(string[] args)
       {
           Thread t1 = new Thread(ThreadNew);
            t1.Name = "NewThread";
// When the isbackground attribute is set to false, the thread newthread can print two messages after the main thread completes execution.
// If isbackground is true, the second message cannot be printed after the main thread ends. Because the main thread (foreground thread) ends, the background thread exits.
            t1.IsBackground = false;
             t1.Start();
Console. writeline ("the main thread has ended ");
        }
  
         static void ThreadNew() {
Console. writeline ("the new thread starts to output messages ");
Thread. Sleep (4000); // The main thread can be terminated 4 seconds earlier than newthread.
Console. writeline ("New thread end Output Message ");
             Console.ReadKey();
      }
   }
}2. applicable foreground and background threads

Generally, the thread that passively listens to the activity should be set as the background thread, and the thread responsible for sending data should be set as the foreground thread, the thread will not be terminated until all data is sent. Background threads should be used only when it is confirmed that the thread is terminated at will without adverse effects. If the thread is executing sensitive or transaction operations that must be completed, or you need to control the way to close the thread to release important resources, use the foreground thread.

For example, there is an example in C # advanced programming-if the word program is closed, it makes no sense that the spelling checker is still running its process. When the application is closed, the spelling checker thread can be closed.

3. asynchronous DelegationAsynchronous delegation: asynchronous calling does not block the main thread, but executes the call in a new thread in the thread pool. We don't have to worry about it or how the new thread is defined.

The asynchronous call of the delegate is implemented through begininvoke and endinvoke.

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.