且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问线程安全的方式C#WPF控件?

更新时间:2022-11-09 09:53:52

您只是想使用Dispatcher.Invoke$c$c>方法(或异步相当于 Dispatcher.BeginInvoke ),这将封送调用主WPF UI线程。

You simply want to use the Dispatcher.Invoke method (or the asynchronous equivalent Dispatcher.BeginInvoke), which will marshal the call to the main WPF UI thread.

的DependencyObject 类包含一个Dispatcher$c$c>属性,这意味着它从该类继承也提供了这个属性,在类似的WinForms方式所有控件和其他对象。此外,应用对象提供接入调度。

The DependencyObject class contains a Dispatcher property, which means all controls and other objects which inherit from this class also provide this property, in a way similar to WinForms. In addition, the Application object provides access to the dispatcher.

使用示例可能是以下(在code-后面的窗口 / 用户控件

An example usage might be the following (in code-behind of a Window/UserControl):

this.Dispatcher.Invoke((Action)(() =>
    {
        ...
    }));