C#如何在多线程里面访问窗体控件和更改控件的属性?在线结贴

mycsdn2008 2004-07-08 05:50:46
如题

请最好给我一段代码,谢谢
...全文
361 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
roselu 2004-07-13
  • 打赏
  • 举报
回复
GZ
marvelstack 2004-07-08
  • 打赏
  • 举报
回复
晚上写的刚好也适合楼主的问题;
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;

}

}

小贵子88 2004-07-08
  • 打赏
  • 举报
回复
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);

}

}
}

我搜到的一段代码,试试吧
mycsdn2008 2004-07-08
  • 打赏
  • 举报
回复
up

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧