Preface : Originally want to summarize according to own experience C # thread related knowledge point, write before read some other people's blog, found oneself also mastered less than one-third .... Hope that through this blog will be their own knowledge point to add, write more straightforward blog and the beginner to share.
This is my reference to the blog address: http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html.
This is his reference to the original English address: http://www.albahari.com/threading/
The original blog introduction can be said in layman's words, so pertinent. However, I would like to write a more suitable for small white, directly take it can be used in the program blog.
I. What is a thread (know the following points to help 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 to the interval between the allocation of a different application, it can also be very smooth to complete processing. As if it can handle a lot of things at the same time, we can do multi-threading, and multi-process programming based on this point.
2. The process is the basic unit of resource allocation. We create a program, which is a process by default, and this process contains a thread (main thread). Now we're going to create a new thread on that basis. (many times this is needed)
3. Threads in a process can share resources in a process.
4. A thread is a unit of dispatch for a system to dispatch and dispatch CPUs independently of a running program.
Two. Why to open a new thread
There are times when the main thread is consuming too much time, causing the mouse not to work and the program looks stuck. At this point, we can put the time-consuming thing into a separate thread, making the main thread easy.
Three. Use of multithreading
1. Target
When you open a thread in the main thread, the thread returns data to the text box in the main form.
The following is all the code, which includes the use of proxies, Invoke, Lock, and so on. This small example will help you get started with threading.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;namespaceThreading Analysis {//create a delegate for SetValue Public Delegate voidSetvaluedel (stringStrObjectobj); Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Thread.CurrentThread.Name="Mainthread"; } //Open a thread Private voidbtnStart_Click (Objectsender, EventArgs e) { //Pass the delegate's method and main form overNewthreadclass Threadoneclass =NewNewthreadclass (SetValue, This); Thread Thethreadone=NewThread (Threadoneclass.threadone);//do not need ThreadStart () can alsoThethreadone.name="Thethreadone";//name the thread for easy debugging. This line of code can be omitted. //Turn the thread into a background thread (by default, the foreground) so that the main thread ends, and it ends. Otherwise, any foreground thread will keep the program alive while it is running. Thethreadone.isbackground =true; Thethreadone.start (); } //assign a value to a text box Private voidSetValue (stringStrObjectobj) { //lock inside the code at the same moment, can only be used by one thread. Queue up behind the rest. This prevents data clutter. Lock(obj) { This. Txtreturnvalue.text = This. Txtreturnvalue.text +str; } } } //build a class that simulates the actual use of Public classNewthreadclass {//receives the delegate method that the main form passes over. PublicSetvaluedel Setvaluedel; //Receive main form Publicform form; //used to tell the main thread where the lock is called. Static ObjectLocker =New Object(); PublicNewthreadclass (Setvaluedel del, Form fom) { This. Setvaluedel =del; This. Form =fom; } //The first thread that passes the value to the control created by the main thread. Public voidThreadone () {//get the name of the thread here stringThreadName =Thread.CurrentThread.Name; Try { while(true) { //tell the main thread that I want to change your control. This. form. Invoke ((EventHandler) (Delegate { //If you use Thread.CurrentThread.Name here, you get the name of the main thread. Setvaluedel (ThreadName +": hello!", locker);//give the text box a value, "own name: hello!". })); Thread.Sleep (3* +);//too tired to rest for three seconds .... } } Catch(Exception ex) {} }}}
C # Threading Analysis (proxy, Invoke, Lock)