Dispatcher.CurrentDispatcher.BeginInvoke未调用 [英] Dispatcher.CurrentDispatcher.BeginInvoke Not Invoking

查看:315
本文介绍了Dispatcher.CurrentDispatcher.BeginInvoke未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FileSystemWatcher ,当监视的文件更改在与UI线程不同的线程上引发时,由此引发的事件。为了避免和跨线程访问乐趣,我尝试使用

I have a FileSystemWatcher and the events raised by this when a watched file changes are raised on a different thread from the UI thread. To avoid and cross-thread acess volation fun, I am attempting to use

public void RaisePathChanged(object sender, RenamedEventArgs e)
{
    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => 
    {
        // Some code to handle the file state change here.
    }));
}

这可以编译,并且 RaisePathChanged 被触发。但是,委托 Action(()=> {/ * Here * /})中的代码永远不会被调用/调用,只是跳过了代码。

This compiles fine and the RaisePathChanged is fired as it should be. However, the code inside the delegate Action(() => { /*Here*/ }) never gets called/invoked, the code is merely skipped.

为什么跳过代码,我该如何解决它,这是确保代码在WPF中创建代码的线程上运行的最佳方法吗?

感谢您的时间。

推荐答案

Dispatcher.CurrentDispatcher Application.Current不同.Dispatcher

第二个是您似乎正在寻找的那个。

The second one is the one you seem to be looking for.

看看这个。

Dispatcher.CurrentDispatcher与Application.Current.Dispatcher

使用应用程序分派器进行试用。

Try it out with application dispatcher.

这篇关于Dispatcher.CurrentDispatcher.BeginInvoke未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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