C # thread analysis (proxy, Invoke, Lock ),

Source: Internet
Author: User

C # thread analysis (proxy, Invoke, Lock ),

Preface: I would like to summarize the knowledge points related to the c # thread based on my own experience. I read some blogs from other people and found that I have mastered less than 1/3 .... I hope to use this blog to add some of my knowledge points and write a more straightforward blog to share with beginners.

This is my reference blog address: http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html.

This is his reference in English original address: http://www.albahari.com/threading/

The introduction of the original blog can be explained in a simple way. However, I want to write a blog that is more suitable for Tom. I can use the blog in the program directly.

 

1. What is thread (know the following points to help you understand)

1. a cpu can only do one thing at a time, but it processes information too quickly, even when we use it, it is divided into a lot of clock cycles (that is, a very short interval), and then allocated to different applications between these intervals, it can also be processed smoothly. Just as it can handle a lot of things at the same time, we can implement multithreading and multi-process programming based on this.

2. process is the basic unit of resource allocation. We create a program, which is a process by default and contains a thread (main thread ). Now we need to create a new thread on this basis. (This is often required)

3. threads in the process can share resources in the process.

4. A thread is the basic unit for independent scheduling and CPU allocation by the system. It refers to the scheduling unit of the running program.

 

Ii. Why should we start a new thread?

In many cases, the main thread consumes too much time to process data, causing the mouse to become unavailable and the program looks stuck. At this time, we can put time-consuming tasks in a separate thread to make the main thread easy.

 

Iii. Multithreading

1. Objectives

Start a thread in the main thread, and the data returned by the thread is displayed in the text box of the main form.

The following is all the Code, including the use of proxy, Invoke, and Lock. This small example can help you start to use the thread.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. threading; namespace thread analysis {// create SetValue delegate public delegate void SetValueDel (string str, object obj); public partial class Form1: Form {public Form1 () {InitializeComponent (); thread. currentThread. name = "MainThre Ad ";} // enable a thread private void btnStart_Click (object sender, EventArgs e) {// pass the delegate method and main form to NEWThreadClass threadOneClass = new NEWThreadClass (SetValue, this); Thread TheThreadOne = new Thread (threadOneClass. threadOne); // ThreadStart () is not required or TheThreadOne. name = "TheThreadOne"; // Name the thread for debugging. This line of code can be omitted. // Change the thread to a background thread (the foreground thread is used by default). The main thread ends and the thread ends. Otherwise, any foreground thread will keep the program alive. TheThreadOne. isBackground = true; TheThreadOne. start () ;}// assign private void SetValue (string str, object obj) to the text box {// the code in lock can be used by only one thread at the same time. Other backend queues. This prevents data confusion. Lock (obj) {this.txt ReturnValue. text = this.txt ReturnValue. text + str ;}}// create a class to simulate the actual use of the public class NEWThreadClass {// receive the delegate method passed by the main form. Public SetValueDel setValueDel; // receives the public Form form of the main Form; // It is used to tell which thread calls the lock in the main thread. Static object locker = new object (); public NEWThreadClass (SetValueDel del, Form fom) {this. setValueDel = del; this. form = fom;} // The first thread to pass the value to the control created by the main thread. Public void threadOne () {// obtain the Thread name string threadName = Thread. currentThread. name; try {while (true) {// tell the main thread that I want to change your control. This. form. Invoke (EventHandler) (delegate {// If Thread. CurrentThread. Name is used here, the Name of the main Thread is obtained. SetValueDel (threadName + ": Hello! ", Locker); // input a value to the text box," your own name: Hello !". }); Thread. Sleep (3*1000); // too tired to rest for three seconds ....} Catch (Exception ex ){}}}}

 

 

 

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.