get filesystemwatcher events to occur on main UI thread

后端 未结 2 977
孤街浪徒
孤街浪徒 2020-12-11 08:14

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

相关标签:
2条回答
  • 2020-12-11 08:49
    this.BeginInvoke((MethodInvoker)(() => SomeMethod())); // Check files in the Main thread otherwise threading issues occur 
    
    0 讨论(0)
  • 2020-12-11 09:07

    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;
    }
    
    0 讨论(0)
提交回复
热议问题