using System.Threading; private void button1_Click(object sender, EventArgs e) { Thread myThread = new Thread(() => { int count =0; for(int i=0;i < 100;i++) { count++; Console.WriteLine(count); System.Threading.Thread.Sleep(1000); } }); myThread.IsBackground = true; myThread.Start(); }
线程中给控件赋值,如下:
private void button2_Click(object sender, EventArgs e) { int count = 0; Thread myThread = new Thread(delegate () { for (int i = 0; i < 100; i++) { count++; Console.WriteLine(count); System.Threading.Thread.Sleep(30); label1.Invoke(new Action<string>(s => { label1.Text = s; }), count.ToString()); } }); myThread.IsBackground = true; myThread.Start(); }