using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(test_show); thread.IsBackground = true; thread.Start(); } private void button2_Click(object sender, EventArgs e) { Thread thread = new Thread(DownloadInWhData); thread.IsBackground = true; thread.Start(); } void test_show() { while (true) { Console.WriteLine("runing test_show"); Thread.Sleep(4000); } } public void DownloadInWhData() { this.Invoke((EventHandler)(delegate { while (true) { Console.WriteLine("runing delegate"); Thread.Sleep(6000); } })); } }
按钮2执行的线程会同步等待这个线程运行完,按钮1不会。