您的位置:首页 > 其它

winform 使用委托 实现多线程访问控件

2013-11-18 09:58 393 查看
private delegate void FlushClient();//代理
private void ThreadFunction()
{
if (this.textBox1.InvokeRequired)
{
FlushClient fc = new FlushClient(ThreadFunction);
this.Invoke(fc);
}
else
{
string s = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒");
this.textBox1.Text = s;
// this.label1.Text = s;
}
}

private void CrossThreadFulush()
{
while (true)
{
Thread.Sleep(1000);
ThreadFunction();
}
}

private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(CrossThreadFulush);
th.IsBackground = true;
th.Start();
}


点击button1执行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: