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);
} }
}我搜到的一段代码,试试吧
{
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);
} }
}我搜到的一段代码,试试吧
解决方案 »
- c#和matlab混合编程时的数据转换问题,关于mwarray和object
- 高分()怎么用把PNG文件 搞成PLT的?这种格式规则是什么啊?
- C#中怎么实现表格绘制,要求能够能够实现单元格合并,打印,表头能够插入特殊字符
- 一个正则表达式,问题,100分
- 如何判断当前程序是由Windows服务启动还是由用户启动?(120分难题求解)
- 一个关于时间显示的问题
- 谁知道有没有c#进程开发方面的书,介绍几本
- winform 如何改变 ToolBar的背景
- 这个类FileNameEditor跑哪儿去了?
- DataGrid能导出pdf文件吗?
- 如何让自定义的控件在设计(VS设计WinForm)时具有不规则的外观。
- 帮忙看看这段用dataset插入多条记录的代码,先谢过!
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; } }