C # difference between foreground thread and background thread

Source: Internet
Author: User

We recommend that you start a background thread button first. Isn't that okay? Then start a foreground thread. Check whether the front-end thread is tenacious and you are not finished! It will never be forgotten!

Using system; using system. drawing; using system. windows. forms; using system. threading; namespace windowsformsapplication1 {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button1_click (Object sender, eventargs E) {// The only difference between the foreground thread and the background thread 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. MessageBox. Show ("click to start a foreground thread. \ R \ n front-end thread: "since I came on stage, I must finish the performance, or I will not finish it with you !" "); // Foreground thread T1 = new thread (say); t1.isbackground = false; t1.name =" foreground thread "; t1.start ();} private void button2_click (Object sender, eventargs e) {MessageBox. show ("click the button to start a background thread. \ R \ n background thread: "Since I didn't come on stage, I am subject to the application. If it is off, I turn it off automatically (the stage is closed, I can't help working in the background )!" "); // Background thread t2 = new thread (say); t2.isbackground = true; t2.name =" background thread "; t2.start ();} private void say () {for (INT I = 0; I <100; I ++) {MessageBox. show ("I play, I play! \ N \ r closed the main window. Are you sure I am not there yet? "+ I. tostring ());}}}}

Friendly reminder: This is a winform program. What is stuck here is the content of the code window, and there is no content in the design field. So you have to create your own project to run the program, pull two buttons, enter the code window, and copy all the copies here to overwrite it and then you can press F5. To save time, the button name is default.

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.