c# - 为什么调度程序 BeginInvoke 在 C# Windows 窗体应用程序中 Control BeginInvoke 成功的地方失败?

标签 c# winforms dispatcher begininvoke

我最初尝试使用 Dispatcher 类 BeginInvoke 方法在我的 C# Windows 窗体应用程序的主 UI 线程上显示消息框。当我使用该方法时,消息框没有出现。我在传递给 BeginInvoke() 的委托(delegate)体内设置了一个断点,它从未被命中。我尝试同时使用 Action 委托(delegate)和 MethodInvoker 委托(delegate)。两种情况都不走运。

当我使用属于 Form 对象的 BeginInvoke 方法时,它工作正常。为什么 Dispatch 版本会悄无声息地失败(没有异常或错误消息)?以下是两个不同的版本。

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

// THIS FAILED. CONTEXT: Executing on worker thread.
MethodInvoker theMethod = new MethodInvoker(delegate()
{
    string msg = "Show this  message on the main UI thread.";
    MessageBox.Show(msg, "Message");
});

dispatcher.BeginInvoke(theMethod);

this.BeginInvoke(theMethod);

// ---------------------------------------------------

// THIS WORKED. CONTEXT: Executing on worker thread.
MethodInvoker theMethod = new MethodInvoker(delegate()
{
    string msg = "Show this  message on the main UI thread.";
    MessageBox.Show(msg, "Message");
});

// "this" is a Form object.
this.BeginInvoke(theMethod);

最佳答案

如果我没看错您的评论,那么您是从非 UI 线程调用 Dispatcher.CurrentDispatcher。这不是它的用途。

作为 Dispatcher.CurrentDispatcher 的文档说:

Gets the Dispatcher for the thread currently executing and creates a new Dispatcher if one is not already associated with the thread.

要获得有效的调度程序实例,您需要从 UI 线程调用 Dispatcher.CurrentDispatcher

另外,因为文档说如果当前线程不存在调度程序,它会自动创建一个调度程序,这就是静默失败的原因。您将获得一个调度程序实例,但它与 UI 线程没有任何关联,因此它实际上并未向 UI 线程调度任何内容。

(删除它,因为在我的测试中,即使我不应该得到 null,所以它似乎并不能证明什么。尽管其余信息是准确的) 文档还添加了:

FromThread 方法不是这种情况。如果没有与指定线程关联的调度程序,FromThread 将返回 null

因此,要确认您确实获得了一个自动创建的(无效的)调度程序,请尝试从 Dispatcher.FromThread 获取调度程序反而。我的猜测是您将得到 null

如果你想调用 dispatcher.BeginInvoke 强制从工作线程执行 UI 线程上的方法,你需要从 UI 调用 Dispatcher.CurrentDispatcher线程并将其保存到变量中。然后,您可以将该调度程序引用变量传递给工作线程,并对其调用 BeginInvoke

// capture and save dispatcher from UI thread
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

// then you can do this from your worker thread:
dispatcher.BeginInvoke(theMethod);

或者,像您已经在做的那样使用 this.BeginInvoke

或者更好的是,您可以尝试将任务与新的 async-await 关键字结合使用来处理此类事情。

编辑

为了完整起见,我应该解释为什么 Control.BeginInvoke 可以正常工作。

作为 Control.BeginInvoke 的文档说:

Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

然后它还添加了:

You can call this method from any thread.

重点是,当您调用 Control.BeginInvoke 时,它不会使用当前线程来确定如何执行委托(delegate)。它会记住控件是在哪个线程(UI 线程)上创建的,并确保在该线程上执行委托(delegate)。

因此,只要您的控件是在 UI 线程上创建的(它应该如此),那么 BeginInvoke 就可以在任何线程中工作。这其实和Dispatcher很相似,只要先从UI线程获取Dispatcher实例,然后就可以调用Dispatcher.BeginInvoke 来自任何线程。

关于c# - 为什么调度程序 BeginInvoke 在 C# Windows 窗体应用程序中 Control BeginInvoke 成功的地方失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31713263/

相关文章:

c# - 线程卡住主 UI

c# - 有没有办法让事件处理程序直接在 UI 线程上运行而无需中间线程池线程?

c# - 控制台应用程序 - 重新创建 Azure CLI `az --login` 的 Web 浏览器本地主机回调身份验证技术

c# - Visual Studio 15.8.5 突然不实时显示错误

c# - PictureBox 图像不会缩小,但 PictureBox 本身会扩展以适合图像

c# - 我如何使用 SetWindowPos 使窗口居中?

c# - WPF 图像滑动以像在 iOS 中一样更改图像

c# - 如何读取HttpResponse Body?

winforms - 从 Visual Studio 2008 安装项目设置 app.config 参数

WPF Dispatcher 不像 BGWorker 那样工作