public class Form1 : System.Windows.Forms.Form
{
  private delegate void UpdateDelegate(string str);

private void button1_Click(object sender, System.EventArgs e)
{
Thread t = new Thread(new ThreadStart(DoTask));
t.Name = "测试";
t.Start();

}
private void DoTask()
{
string str = Thread.CurrentThread.Name;

UpdateDelegate eh = new UpdateDelegate(UpdateText);
this.textBox1.Invoke(eh,new object[]{str});
}

private void UpdateText(string args)
{

this.textBox1.AppendText(args);

} }
}我搜到的一段代码,试试吧

解决方案 »

  1.   

    晚上写的刚好也适合楼主的问题;
    http://blog.csdn.net/zhzuo/archive/2004/07/08/37262.aspx主要就是调用
    Control.Invoke 方法
    或异步调用
    Control.BeginInvoke 方法需要定义方法的委托
    public delegate void PingCompletedHandler(object sender,PingEventArgs e);
     //更新信息显示,Invoke异步调用         private void pingCommand_PingCompleted(object sender,PingEventArgs e)         {              this.listView1.BeginInvoke(new UpdateListViewHandler(MessageInfoShow),new object[]{e.PingResult});         }         //更新ListView         private void MessageInfoShow(string[,] pingResult)         {              this.listView1.Items.Insert(0,new ListViewItem(new string []{pingResult[0,0],pingResult[0,1]}));              if(this.listView1.Items.Count ==this.hostCount)              {                   this.listView1.Refresh();                   TimeSpan ts = (TimeSpan)(DateTime.Now-this.startTime);                   this.labelTime.Text = "检测时间:"+ts.TotalSeconds.ToString()+"秒";                   this.buttonPing.Enabled = true;              }         }