如何将此代码“翻译”为WPF

时间:2013-06-09 12:15:34

标签: wpf multithreading

我将应用程序从WindowsForms移动到WPF。我有以下代码:

        if (InvokeRequired)
        {
            Invoke(new Action<Version, Version, XDocument>(ShowUpdateDialog), appVersion, newVersion, doc);
            return;
        }

如何在WPF上编写相同的代码?感谢

1 个答案:

答案 0 :(得分:2)

你真的没有给出足够的背景信息。在过去的Windows窗体中,当我想从另一个线程执行与UI相关的操作时,我会检查InvokeRequired是否为真,WPF中的等价物是:

Application.Current.Dispatcher.Invoke(new Action<Version, Version, XDocument>(ShowUpdateDialog), appVersion,
                                              newVersion, doc);

这会将操作推送到将同步执行的UI线程,如果从UI线程本身调用它,则不会导致问题。