FileSystemWatcher OnChanged事件需要重新加入UI线程 [英] FileSystemWatcher OnChanged event needs to rejoin UI thread
问题描述
一旦OnChanged事件完成,如何在FileSystemWatcher中获取OnChanged事件以在UI线程上调用方法?
How can I get the OnChanged event in a FileSystemWatcher to invoke a method on the UI thread once the OnChanged event is completed?
或者,仅出于知识上的考虑,如何使OnChanged事件完全在UI线程上运行?
Or, just for the sake of knowledge, how can I get the OnChanged event to run entirely on the UI thread?
推荐答案
如果您希望 FileSystemWatcher
在UI线程上引发事件,并且您正在使用Windows窗体,请设置 SynchronizingObject 代码>到控件或窗体.如果您使用的是WPF(没有实现
ISyncronizeInvoke
的任何功能),则此功能就没有用.例如,通常您只需通过 Application.Current.Dispatcher.Invoke
在Dispatcher上调用Invoke.
If you want FileSystemWatcher
to raise events on the UI thread and you are using Windows Forms, set the SynchronizingObject
to a Control or Form. This isn't as useful if you are using WPF, where there isn't anything that implements ISyncronizeInvoke
. You typically just call Invoke on the Dispatcher via Application.Current.Dispatcher.Invoke
, for example.
这篇关于FileSystemWatcher OnChanged事件需要重新加入UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!