C # simple use of multithreading and common parameters,
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; using System. threading; // The thread operation class is in this namespace. namespace C02 multithreading {public partial class Form1: Form {public Form1 () {InitializeComponent (); // disable the control's cross-thread access check TextBox. checkforillegalcrossthreadcils = false;} private void button#click (object sender, EventArgs e) {int sum = 0; for (int I = 0; I <999999999; I ++) {sum + = I;} MessageBox. show ("OK");} private void button2_Click (object sender, EventArgs e) {// create a thread object and specify the method to be executed for this thread object. thread thread = new Thread (TestThread); // sets the Thread as the background thread. thread. isBackground = true; // enable thread. start (); // by default, all threads are foreground threads. // The program will exit after all foreground threads exit. // The background thread ends immediately as long as all foreground threads end the background thread. // The default thread in the process is called the main thread or the UI thread. // when will the thread automatically exit after the thread finishes executing the method. // when multiple threads access the same resource, synchronization may occur. this is called thread re-import. // th. abort (); force stop the thread. // Thread. sleep (1000); // pause the current Thread in milliseconds // Thread. currentThread; get the reference of the current thread // call the method with parameters // create a ParameterizedThreadStart delegate object. bind the method to this delegate object. // pass the delegate object to the thread object through the constructor // call the overload of Start () when starting the thread to pass the parameter .} // prepare the thread for calling. private void TestThread () {int sum = 0; for (int I = 0; I <999999999; I ++) {sum + = I;} MessageBox. show ("OK");} private void CountNunm () {// use lock to lock your toilet. Please think about the situation of lock (this) {for (int I = 0; I <10000; I ++) {int num = int. parse (textBox1.Text. trim (); num ++; // Thread. sleep (500); // pause the current Thread // Thread. currentThread. abort (); // Thread th = Thread. currentThread; // th. abort (); // if (num = 5000) // {// th. abort (); //} textBox1.Text = num. toString () ;}} Thread th; private void button3_Click (object sender, EventArgs e) {th = new Thread (CountNunm); th. name = "Haha thread"; th. isBackground = true; th. start (); // Thread th1 = new Thread (CountNunm); // th1.IsBackground = true; // th1.Start ();} private void button4_Click (object sender, EventArgs e) {// ThreadStart s = new ThreadStart (CountNunm); // Thread th = new Thread (CountNunm); ParameterizedThreadStart s = new ParameterizedThreadStart (TestThreadParsms ); thread t = new Thread (s); t. isBackground = true; t. start ("hello");} private void TestThreadParsms (object obj) {Console. writeLine (obj. toString ());}}}