Thread:
Help the boss work, do not interfere with the boss's normal job
If the execution of a piece of code takes time, a new thread must be opened to execute,
If you do not thread, the window will appear suspended animation
Thread:
Thread th = new thread (delegate);
Th. IsBackground = true; Setting up a background thread
Th. Start ();
Threads are not allowed to access objects across threads by default
Use Control.checkforillegalcrossthreadcalls = False in the constructor; You can allow access to objects across threads.
To close a thread:
Th. Abort ();
A thread can execute a method of at most one parameter, which must be of type Object
Use a thread to do a three-level connection to a small program:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceThread Contact __ Applet ___2017_5_4{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Control.checkforillegalcrossthreadcalls=false; } Thread th=NULL; Thread Th1=NULL; Thread Th2=NULL; Private voidButton1_Click (Objectsender, EventArgs e) {th=NewThread (test1); Th. IsBackground=true; Th. Start (); Th1=NewThread (test2); Th1. IsBackground=true; Th1. Start (); Th2=NewThread (TEST3); Th2. IsBackground=true; Th2. Start (); } Private voidButton2_Click (Objectsender, EventArgs e) {th. Abort (); Th1. Abort (); Th2. Abort (); } Public voidtest1 () {Random R=NewRandom (); string[] name =New string[] {"Zheng","Xu Qiang","son June","Zi Xuan","Luo Wei" }; while(true) { intA = R.next (1, name. Length); TextBox1.Text=Name[a]; Thread.Sleep ( -); } } Public voidtest2 () {Random R=NewRandom (); string[] City =New string[] {"Boxing","Zhangdian","Maqiao","Jinan","Qingdao" }; while(true) { intb = R.next (1, City. Length); TextBox2.Text=City[b]; Thread.Sleep ( -); } } Public voidtest3 () {Random R=NewRandom (); string[] Events =New string[] {"Eat Hamburgers","Travel","Naughty","School","Write code","Play" }; while(true) { intc = R.next (1, events. Length); TextBox3.Text=Events[c]; Thread.Sleep ( -); } } }}
2017-5-4 Threads