usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Threading;//The class of the thread operation is under this namespace.namespaceC02 Multithreading { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); //Close cross-thread access checks for controlsTextbox.checkforillegalcrossthreadcalls =false; } Private voidButton1_Click (Objectsender, EventArgs e) { intsum =0; for(inti =0; I <999999999; i++) {sum+=i; } MessageBox.Show ("OK"); } Private voidButton2_Click (Objectsender, EventArgs e) { //Create 1 Thread objects and specify the method to be executed for this thread object.Thread thread =NewThread (Testthread); //sets the thread as a background thread.Thread. IsBackground =true; //Open Threadthread. Start (); //threads are foreground threads by default. //the program will not exit until all foreground threads have exited. //the background thread ends immediately as long as all foreground threads end the background thread. //the default threads in the process are called the main thread or the UI thread. //The thread automatically exits when the thread finishes execution of the thread when it is finished. //multiple threads accessing the same resource can cause an out-of-sync situation. This is called thread re-entry. //th. Abort (); Forcibly stops the thread. //Thread.Sleep (+);//pauses the current thread in milliseconds//Thread.CurrentThread; Get a reference to the current thread//thread calls a method with parameters//creates 1 Parameterizedthreadstart delegate objects. Binds the method to this delegate object .//Passing a delegate object through a constructor to a thread object//when the thread is started, an overload of start () is called to pass in the argument. } //ready for the thread to call. Private voidTestthread () {intsum =0; for(inti =0; I <999999999; i++) {sum+=i; } MessageBox.Show ("OK"); } Private voidcountnunm () {//using the lock locking, please remind me of your toilet. Lock( This) { for(inti =0; I <10000; i++) { intnum =int. Parse (TextBox1.Text.Trim ()); Num++; //Thread.Sleep (+);//pauses the current thread//Thread.CurrentThread.Abort (); //Thread th = thread.currentthread; //th. Abort (); //if (num = =)//{ //th. Abort (); //}TextBox1.Text =Num. ToString (); }}} the Thread th; Private voidButton3_Click (Objectsender, EventArgs e) {th=NewThread (COUNTNUNM); Th. Name="haha thread"; Th. IsBackground=true; Th. Start (); //thread Th1 = new Thread (COUNTNUNM); //Th1. IsBackground = true; //Th1. Start (); } Private voidButton4_Click (Objectsender, EventArgs e) { //ThreadStart s = new ThreadStart (COUNTNUNM); //thread th = new Thread (COUNTNUNM);Parameterizedthreadstart s =NewParameterizedthreadstart (testthreadparsms); Thread T=NewThread (s); T.isbackground=true; T.start ("Oh, hello."); } Private voidTestthreadparsms (Objectobj) {Console.WriteLine (obj. ToString ()); } }}
C # Multithreading simple use method and common parameters