[英]How do you set a component property from within a thread without throwing an error in C# .NET?
I have a user interface which needs to be updated by a function running in a backgroundworker
(a different thread from the primary one). 我有一个用户界面,需要通过在
backgroundworker
工作程序中运行的函数(与主要工作程序不同的线程)进行更新。
I would like to effectively be able to do the following without throwing an exception: 我希望能够在不抛出异常的情况下有效地执行以下操作:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
MyProgresssbar.value = val;
MyLabel.text = "Still going...";
}
For example, could a delegate function be written that sets a component property (I don't want to have a function for each property change). 例如,可以编写一个委托函数来设置一个组件属性(我不希望每个属性更改都有一个函数)。
Thanks in advance. 提前致谢。
You should use BackgroundWorker's built-in progress reporting functionality. 您应该使用BackgroundWorker的内置进度报告功能。
As a more general answer, you can run code in the UI thread (aynchronously) by calling 作为更一般的答案,您可以通过调用在UI线程中运行代码(异步)
someControl.BeginInvoke(new Action(delegate { ... }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.