【. NET Foundation "--delegates, events, Threads (3)

Source: Internet
Author: User

In the previous two articles we learned about delegates and events, and we looked at threads in this article.

1, a form program that has a single thread by default(equivalent to a store with only one clerk), this default thread is called the UI thread/main thread. 2, process and thread relationships:

A, process, which contains the resources required to run the program, in most cases refers to the program. (Store: A place to hoard resources to use)

B, a thread, a program unit that can be called by the CPU in a process, is a snippet of code that is provided to the CPU to run the program. (Store employee: The person who runs the program)

C, a process at least one thread, each thread has its own register (stack pointers, program counters, etc.) but the code area is shared, different threads can execute the same function

D, can be "concurrent" execution between multiple threads in the same process

3, the purpose of multithreading:

A, let the CPU actively execute different program units, so as not to be a program of malicious code caused by the crash symptoms

B, let the computer "simultaneously" do many things, save time

C,cpu in different threads, switching between different processes

5, the way the thread is dispatched:

 A, non-preemptive scheduling: refers to a thread in the process of operation will not be forced to suspend the operating system, the thread can continue to run until the time or the active surrender of the operation. A thread runs entirely on a single queue (like ordering a ticket), which can result in a long-term run-off of malicious programs, and once a program dies, the computer can only be restarted.

 B, preemptive scheduling: Each thread has very little running time (in Windows kernel mode this time will not exceed 20ms), when the end of the run-time thread will be forced to pause, save the context and the CPU to the next thread of operation, The result of this dispatch is that all the threads are running quickly, so that the client feels that the thread is running "parallel".

C, the invocation of the thread is determined by the CPU, so calling the start method of the thread instance marks that the thread can be executed by the CPU, but the specific execution time is determined by the CPU.

6, the thread switches to save the current execution state of the thread, that is, the thread's current execution session

The register in the thread has the code number currently executing, and the stack stores the value of the currently running variable. When CPU execution returns to this thread again, the data stored in the previous register and stack is read.

program code, compiled later is the instruction set of the CPU, the CPU performs read-only operations on the specified set. Such as:

 

7, how to implement multithreading?

A, write the method that produces the thread that needs to be executed

B, referencing the System.Threading namespace

C, instantiate the thread class and pass in a delegate that points to the method that the thread needs to run (the thread has already been generated and has not started running)

D, call the Start method of the tread instance, marking that the thread can be executed by the CPU (the specific execution time is determined by the CPU)

 /// <summary>        ///Multithreading/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtnthread_click (Objectsender, EventArgs e) {            //1, creating threads and passing the method through the delegate syntax sugarThread Thrson =NewThread (COUNTDO); //2, set as Background threadThrson.isbackground =true; //3, start threadThrson.start (); }        voidCountdo () {inti =0;  while(true)            {                if(I <999999999) {i++; }                Else                {                     Break; }} MessageBox.Show ("Calculation completed:"+i); }
View Code8, what exactly is a thread?

A thread is a storage unit, a space stored in memory, a task list is stored, what to do! These tasks are given to the CPU to do. The CPU decides when to do it.

A thread is the underlying data type that helps the CPU to complete the steals execution, and is used to store code execution information that needs to be saved each time the CPU performs an incomplete execution. (such as: Which method is being executed?) How many lines have you implemented? What is the value of the variable in the method? )

Some important members of the 9,thread class
    • Start () Startup thread
    • Abort () Terminate thread
    • Thread.Sleep (100) static method that allows the current thread to stop running for a period of time (MS)
    • Name Thread Name
    • Thread.CurrentThread getting a reference to the current thread
    • foreground thread and background thread
10, multi-threaded consumption

Switching between threads requires saving the current execution state, as well as reading the execution state before the switch, the multithreading consumption.

【. NET Foundation "--delegates, events, Threads (3)

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.