问题
How can I get the OnChanged event in a FileSystemWatcher to invoke a method on the UI thread once the OnChanged event is completed?
Or, just for the sake of knowledge, how can I get the OnChanged event to run entirely on the UI thread?
回答1:
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.
来源:https://stackoverflow.com/questions/28036423/filesystemwatcher-onchanged-event-needs-to-rejoin-ui-thread