BackgroundWorker的多线程访问形式 [英] BackgroundWorker multithread access to form

查看:155
本文介绍了BackgroundWorker的多线程访问形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的同时有一定目的的运行5 BackgroundWorker的对象,所有的人都更改相同的标签。我该怎么做呢?

I am using 5 BackgroundWorker objects running at the same time for a certain purpose, and all of them have to change the same label. How do I do that?

如何修改然后从多个线程的形式?我如何做到这一点的情况下,我想改变一个公共字符串?

How do I modify the form from more than one thread then? And how do i do it in case i want to change a public string?

推荐答案

使用Control.Invoke了委托。

Use Control.Invoke with a delegate.

在你的背景工作者线程,而不是说

In your background worker thread, instead of saying

label4.Text = "Hello";



say

label4.Invoke(new Action(() =>
{
  label4.Text = "Hello";
}
));



里面的一切控件的线程上{}执行,让您避免例外。

Everything inside the { } executes on the control's thread, so you avoid the exception.

这允许你这样做从的BackgroundWorker ,而不是仅仅报告进度的用户界面任意更改。

This allows you to do arbitrary changes to your user interface from a BackgroundWorker rather than just reporting progress.

这篇关于BackgroundWorker的多线程访问形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆