get filesystemwatcher events to occur on main UI thread

北慕城南 提交于 2020-01-10 04:19:33

问题


Can i get filesystemwatcher events to occur on the main UI thread ?. Currently file changes are fired off on their own threads.


回答1:


Simply set the FileSystemWatcher.SynchronizingObject property to the form instance. Same thing as calling BeginInvoke() but done automatically for you. Boilerplate code:

public Form1() {
    InitializeComponent();
    fileSystemWatcher1.SynchronizingObject = this;
}



回答2:


this.BeginInvoke((MethodInvoker)(() => SomeMethod())); // Check files in the Main thread otherwise threading issues occur 


来源:https://stackoverflow.com/questions/22465167/get-filesystemwatcher-events-to-occur-on-main-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!