日期:2014-05-17 浏览次数:20906 次
private void button2_Click(object sender, EventArgs e) { waitCallback = new WaitCallback(MyThreadWork); ThreadPool.QueueUserWorkItem(waitCallback, "第一个线程"); ThreadPool.QueueUserWorkItem(waitCallback, "第二个线程"); ThreadPool.QueueUserWorkItem(waitCallback, "第三个线程"); ThreadPool.QueueUserWorkItem(waitCallback, "第四个线程"); } public void MyThreadWork(object state) { int i=0; while (i<20) { i++; SetText(string.Format("线程现在开始启动…… {0}", (string)state)); Thread.Sleep((new Random(5)).Next()*1000); SetText(string.Format("运行结束…… {0}", (string)state)); } } public delegate void mydeleg(string str); public void SetText(string str) { if (textBox1.InvokeRequired) { textBox1.Invoke(new mydeleg(SetText),str); } else textBox1.Text = str; }