. NET Learning notes----2015-07-03 (threads/multithreading)

Source: Internet
Author: User

Problems with single thread

If a program does more than one thing, a "stuck" state will appear.

Why use Multithreading:

1, let the computer "at the same time" do more things, save time

2. Multithreading allows a program to "simultaneously" handle multiple things

3. Run the program in the background, improve the operation efficiency of the program, and will not make the main interface unresponsive situation

4. Get current thread and current process

Thread is divided into foreground thread and background thread

1. Foreground thread: Only all foreground threads are closed to complete the program shutdown

2, Background thread: As long as all the foreground thread ends, the background thread automatically ends

Steps to produce a single thread

1, write the method that produces the thread to execute

2. System.Threading namespace

3. Instantiate the thread class and pass in a method delegate that points to the thread to run (at which time the thread has been generated but not yet running)

4. Call the Start method of the thread instance, marking that the thread can be executed by the CPU, and the specific execution time is determined by the CPU

Cross-thread access is not allowed under. Net

The thread cannot start again after being abort (the method of shutting down the thread).

Thread.Sleep (ms), static method that enables the current thread to stop running for a period of time

Name Thread Name

Thread.CurrentThread getting the current thread reference

        //Create a thread to execute this methodThread th; Private voidButton1_Click (Objectsender, EventArgs e) {            //Create a thread to execute this methodth =NewThread (Test); //flag This thread is ready to be executed at any time. Exactly when to execute this thread, determined by the CPU//To set a thread as a background threadTh. IsBackground =true; Th.            Start (); //Test ();        }        Private voidTest () { for(inti =0; I <10000; i++)            {                //Console.WriteLine (i);TextBox1.Text = i.ToString ();//This will cause cross-threading operations, reporting exceptions            }        }        Private voidForm1_Load (Objectsender, EventArgs e) {            //to cancel access across threadsControl.checkforillegalcrossthreadcalls =false; }        Private voidForm1_formclosing (Objectsender, FormClosingEventArgs e) {            //When you click Close Main form, determine if the new thread is null, and if it is not NULL, manually close the            if(Th! =NULL)            {                //End this threadth.                Abort (); //the thread cannot start again after it has been abort.            }        }    }

. NET Learning notes----2015-07-03 (threads/multithreading)

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.