c#使用MethodInvoker解决跨线程访问控件
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用MethodInvoker即可解决:
原代码:
private void btnOK_Click(object sender, EventArgs e)
{
tslInfo.Text = "请稍候...";
Thread td = new Thread(new ThreadStart(run));
td.Start();
}
/// <summary>
/// 线程方法
/// </summary>
private void run()
{
this.tslInfo.Text = "就绪";
}
修改后:
private void btnOK_Click(object sender, EventArgs e)
{
tslInfo.Text = "请稍候...";
Thread td = new Thread(new ThreadStart(threadRun));
td.Start();
}
/// <summary>
/// 原线程方法
/// </summary>
private void run()
{
this.tslInfo.Text = "就绪";
}
/// <summary>
/// 线程方法
/// </summary>
private void threadRun()
{
MethodInvoker In = new MethodInvoker(run);
this.BeginInvoke(In);
}