- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最初尝试使用 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/
Yii::$app->runAction('new_controller/new_action', $params); 我相信这可以用来从另一个 Controller 调用 Controller Ac
这个问题类似于 this ,但我需要访问父成员(不是控制)。我不知道是否可以不使用依赖注入(inject)。 例如,我有一个父级,有一个成员调用用户,我需要从子 Controller 访问用户。 最佳
我有包含 2 个布局的根布局:- 选项面板- 绘制区域 我正在尝试的是访问 OptionsPaneController 中的 DrawAreaController 以调用其绘制方法。下面是 Optio
我的应用程序的 View Controller 层次结构设置如下: UIViewController | UITabBarController | UINavigationCo
我的应用程序的 View Controller 层次结构设置如下: UITabBarController | UINavigationController | | |
当我第一次为我目前在 Storyboard 中开发的应用程序创建基础布局时,我分两步完成: 选择我的 View Controller 并使用 Editor->Embed In->Navigation
设计要求: 显示用户可以选择的项目列表 选择一个项目后,使用后退按钮将用户带到一个新 View 。新 View 应在底部包含第一个屏幕中不存在的选项卡列表 单击选项卡中的项目时,应出现一个带有后退按钮
将父 Controller 设置为“parentCtrl as vm”,并将子 Controller 设置为“childCtrl as vmc”,以避免名称冲突,并且效果良好。 如何在子 Contro
我已经阅读了一些答案,例如关闭当前的 ViewController,但我的情况有所不同,因为我正在展示另一个 ViewController。 虽然我无法访问它的属性,但此代码显示了带有导航 Contr
如我所见,如果我们要实例化一个Model(例如,名为Post),我们只需调用: $post = new Post(); 现在,我还想实例化一个Controller(例如,名为Post,并为此 Cont
我已经疯狂地在整个网络上搜索解决我的问题的方法,但目前还没有。我的问题是我必须检查是否在 HTTP 请求中获得特定文本,该请求在一个 while 循环中,如果我这样做了,那么我应该离开循环并继续线程,
我想用this.get('controllers.pack.query');要得到App.PackQueryController在 App.PackController ,但失败了。 我认为问题是 E
我刚开始使用 Laravel。当我使用 codeigniter 或 zend 框架时,我可以将我的 Controller 组织到一个单独的目录中。例如,我可以创建“user/permission.ph
在 emberjs 前 2 我们可以从另一个 Controller 访问 Controller 或 Controller 中的任何方法 以下方式: App.get('router').get('nav
这可能是非常简单的实现,但我是 iOS 编程的新手,我似乎被卡住了。 所以,基本上,我有一个选项卡式应用程序。我决定除了标签栏之外还需要一个导航栏。为此,我放置了标签栏 Controller ,然后添
我有这个列表 Controller , define([ 'jquery', 'app' ], function ($,app) { app.controller("ListC
我有 3 个 Controller :RootController、FirstController 和 SecondController。我想从 RootController -> FirstCont
我有以下 Controller : /controllers/api/base_controller.rb /controllers/api/v1/articles_controller.rb 当为文
我是 Angular JS 的新手,尝试在另一个 Controller 中调用一个 Controller ,但出现以下错误。 ionic.bundle.js:21157 TypeError: $con
我有一个标签栏 Controller 和它的 3 个 child ,我还有另一个 View ,我制作了一个从 child 到 View Controller 的自定义转场,还有一个从 View Con
我是一名优秀的程序员,十分优秀!