wpf Dispatcher.CurrentDispatcher.BeginInvoke 未调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20148541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dispatcher.CurrentDispatcher.BeginInvoke Not Invoking
提问by MoonKnight
I have a FileSystemWatcherand 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
FileSystemWatcher当被监视的文件更改在与 UI 线程不同的线程上引发时,我有一个和由此引发的事件。为了避免和跨线程访问 volation 的乐趣,我试图使用
public void RaisePathChanged(object sender, RenamedEventArgs e)
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
// Some code to handle the file state change here.
}));
}
This compiles fine and the RaisePathChangedis fired as it should be. However, the code inside the delegate Action(() => { /*Here*/ })never gets called/invoked, the code is merely skipped.
这编译得很好,并且按RaisePathChanged原样被触发。然而,委托中的代码Action(() => { /*Here*/ })永远不会被调用/调用,代码只是被跳过。
Why is the code being skipped, how can I fix it and is this the best way to insure code is run on the thread that created it in WPF?
为什么跳过代码,我该如何修复它,这是确保代码在 WPF 中创建它的线程上运行的最佳方法吗?
Thanks for your time.
谢谢你的时间。
回答by dev hedgehog
You are mixing up things.
你把事情搞混了。
Dispatcher.CurrentDispatcheris not the same as Application.Current.Dispatcher.
Dispatcher.CurrentDispatcher不一样Application.Current.Dispatcher。
The second one is the one you seem to be looking for.
第二个是您似乎正在寻找的那个。
Take a look at this.
看看这个。
Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher
Dispatcher.CurrentDispatcher 与 Application.Current.Dispatcher
Try it out with application dispatcher.
使用应用程序调度程序尝试一下。
回答by ILIA BROUDNO
Dispatcher.CurrentDispatcheris the dispatcher of the "current" thread - in this case the thread RaisePathChangedis executing on.
When you say Dispatcher.CurrentDispatcher.NET will create a new dispatcher if there was none.
It will however not Run()said dispatcher!
So when you schedule something on it (with BeginInvoke) it will not actually be executed unless that dispatcher is running.
That likely answers your first question (Why is it not Invoking?)
To avoid cross-thread access violation, you need the dispatcher of the thread that created whatever you are trying to protect and make sure it is a running dispatcher.
If whatever you are trying to protect was created on the default GUI thread, then use Application.Current.Dispatcherlike the previous answer says, otherwise you will need to do a bit more explaining and post a bit more code before we can answer your second question. http://www.diranieh.com/NET_WPF/Threading.htmhas a pretty short intro into the subject.
Dispatcher.CurrentDispatcher是“当前”线程的调度程序 - 在这种情况下,线程RaisePathChanged正在执行。
当您说Dispatcher.CurrentDispatcher.NET 将创建一个新的调度程序时,如果没有。
然而它不会Run()说调度员!
因此,当您在其上(使用BeginInvoke)安排某些内容时,除非该调度程序正在运行,否则它实际上不会被执行。
这可能回答了您的第一个问题(为什么不调用?)
为了避免跨线程访问冲突,您需要创建您要保护的任何内容的线程的调度程序,并确保它是正在运行的调度程序。
如果您尝试保护的内容是在默认 GUI 线程上创建的,则使用Application.Current.Dispatcher就像之前的答案所说的那样,否则您需要做更多的解释并发布更多的代码,然后我们才能回答您的第二个问题。http://www.diranieh.com/NET_WPF/Threading.htm对这个主题有一个非常简短的介绍。

