如何从其他线程更新UI? [英] How to update UI from other threads?

查看:70
本文介绍了如何从其他线程更新UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的WPF应用程序时,我想做下面给出的要点



>一个接一个地执行3个方法(MethodA,MethodB,MethodC)。

>窗口中的标签控件,显示现在正在执行的方法。(例如:执行方法A ......)

>显示窗口无需等待。(UI线程不能等待其他3种方法完成)。

怎么做?

When i run my WPF application, i want to do the points given below

> Execute 3 methods(MethodA,MethodB,MethodC) one after another.
> A label control in the Window which shows which method is executing now.(eg: Executing MethodA...)
> Show window without waiting.(UI thread must not wait for other 3 methods to finish).
How to do this?

推荐答案

你无法从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke()

使用Treeview扫描仪和MD5的问题



另请参阅有关线程的更多参考资料:

主要的.NET事件线程 [ ^ ],

主线程上的.NET事件

如何在vb.net中的其他线程上运行keydown事件
启用禁用后控制事件未触发+多线程



You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
.NET event on main thread[^],
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

Prathap写道:
Prathap wrote:



...要在标签控件中显示文本,我在BackgroundWorker的ProgressChanged事件中编写了代码并且工作正常。

我应该何时使用 Control.Invoke

每次需要在UI中更改内容时都需要调用它。例如,要更改标签文本,您可以使用以下方法:

You need to call it each time you need to change something in UI. For example, to change the label text, you could use this method:

static void SetLabelText(Label label, string newText) {
    label.Content = newText;
}

但这是你只能从同一个UI线程调用的东西。正如我之前已经解释的那样,你不能从另一个线程调用。因此,您需要在所有情况下从UI线程调用它,但在另一个线程中该怎么做? 将其委托给UI线程。这是怎么回事?



```cs

使用System.Windows ;

使用System.Windows.Controls;



// ...



公共部分类MyWindow:Window {



public MainWindow(){

InitializeComponent();

} // MainWindow



void SetLabelText(标签标签,字符串newText){

Dispatcher.Invoke(

new System.Action< label,string =>((aLabel,aText)=> {

aLabel.Content = aText;

}),label ,newText);

} // SetLabelText



// ...



} // class MyWindow

```



-SA

But this is something you could only call from the same UI thread. As I already explained before, you cannot call from a different thread. So, you need to call it from UI thread in all cases, but what to do in another thread? Delegate it to the UI thread. This is how?

```cs
using System.Windows;
using System.Windows.Controls;

// ...

public partial class MyWindow : Window {

public MainWindow() {
InitializeComponent();
} //MainWindow

void SetLabelText(Label label, string newText) {
Dispatcher.Invoke(
new System.Action<label, string="">((aLabel, aText) => {
aLabel.Content = aText;
}), label, newText);
} //SetLabelText

// ...

} //class MyWindow
```

—SA


1)
private void MyFunction()
   {
   MethodA();
   MethodB();
   MethodC();
   }





2)



2)

private void MethodA()
   {
   myLabel.Text = "MethodA";
   }
private void MethodB()
   {
   myLabel.Text = "MethodB";
   }
private void MethodC()
   {
   myLabel.Text = "MethodC";
   }



3)将MyFunction移动到 BackgroundWorker [ ^ ]并使用调用 [ ^ ]设置标签文本。


3) Move MyFunction into a BackgroundWorker[^] and use Invoke[^] to set the label text.


这篇关于如何从其他线程更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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