[. NET Basics]-delegate, event, thread (3),. net delegate

Source: Internet
Author: User

[. NET Basics]-delegate, event, thread (3),. net delegate

In the previous two articles, we have learned about delegation and events. In this article, we will take a look at the thread.

1. A form program has one thread by default (equivalent to a store with only one clerk). This default thread is called the UI thread/main thread. 2. Relationship between processes and threads:

A. A process contains the resources required for running the program. In most cases, it refers to A program. (Store: the place where resources to be used are hoarded)

B. A thread is a program unit that can be called by the CPU in the process. It is a code segment provided to the CPU running program. (Store employee: The operator of the Program)

C. A process has at least one thread. Each thread has its own register (Stack pointer, program counter, etc.) but the code zone is shared. Different threads can execute the same function.

D. multiple threads in the same process can be executed concurrently.

3. multi-thread purpose:

A. Let the CPU actively execute different program units, so that it will not be caused by malicious code of A program.

B. Let the computer "do multiple things at the same time" to save time

C. Switch the CPU in different threads and in different processes

5. Thread Scheduling Method:

 A. Non-preemptible scheduling:It means that a thread will not be forcibly paused by the operating system during the running process, and the thread can run until it reaches an end or voluntarily hand over the running right. Threads run in a single Queue (like buying tickets in order), which may result in a situation where malicious programs occupy the operation right for a long time. Once a program dies, the computer can only restart.

 B. preemptive scheduling:Each thread has a very short running time (in Windows kernel mode, this time will not exceed 20 ms). When the time is used up, the thread will be forcibly paused, save the context and give the CPU running permission to the next thread. In this way, the scheduling result is that all threads are quickly switching and running, this gives the client the feeling that the thread is running in parallel at the same time.

C. The CPU determines the Thread call. Therefore, the Start method of the Thread instance is called to mark that the Thread can be executed by the CPU, but the specific execution time is determined by the CPU.

6. The current execution status of the thread is saved during thread switching, that is, the current execution Session of the thread.

The registers in the thread have the code number currently executed, and the stack stores the value of the currently running variable. When the CPU runs back to this thread again, it reads the data stored in the previous register and stack.

The program code is compiled into the CPU instruction set. The CPU performs read-only operations on the specified set. For example:

 

7. How to Implement multithreading?

A. Compile the method to be executed to generate the thread

B. reference the System. Threading namespace.

C. instantiate the Thread class and pass in a delegate pointing to the method required by the Thread (the Thread has been generated and has not started running)

D. Call the Start method of the Tread instance to mark 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 void btnThread_Click (object sender, eventArgs e) {// 1. Create a Thread and use the delegate syntax sugar to input the Thread thrSon = new Thread (CountDo); // 2, set it to the background Thread thrSon. isBackground = true; // 3, the startup thread thrSon. start ();} void CountDo () {int I = 0; while (true) {if (I <999999999) {I ++;} else {break;} MessageBox. show ("computing completed:" + I );}View Code 8. What is the thread?

A thread is a storage unit, a storage space in the memory, and a task list. What should I do! These tasks are handed over to the CPU. CPU determines when to do this.

A thread is the basic data type that helps the CPU to complete the preemptive execution. It is used to store the code execution information that the CPU needs to save when each execution is incomplete. (For example, which method is being executed? What lines have you executed? Why is the variable value in the method ?)

9. Some important members of the Thread class
  • Start () Start thread
  • Abort () terminate a thread
  • Thread. Sleep (100) static method, which can stop the current Thread for a period of time (milliseconds)
  • Name thread Name
  • Thread. CurrentThread to get the reference of the current Thread
  • Foreground thread and background thread
10. multithreading consumption

To switch between threads, you need to save the current execution status and read the execution status before the switch. This is the consumption of multiple threads.

Related Article

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.