C# 获取要在主UI线程上发生的filesystemwatcher事件

C# 获取要在主UI线程上发生的filesystemwatcher事件,c#,winforms,filesystemwatcher,C#,Winforms,Filesystemwatcher,我可以让filesystemwatcher事件发生在主UI线程上吗?。当前,文件更改在它们自己的线程上触发。只需将属性设置为表单实例。与调用BeginInvoke()相同,但会自动完成。样板代码: this.BeginInvoke((MethodInvoker)(() => SomeMethod())); // Check files in the Main thread otherwise threading issues occur public Form1() { Ini

我可以让filesystemwatcher事件发生在主UI线程上吗?。当前,文件更改在它们自己的线程上触发。

只需将属性设置为表单实例。与调用BeginInvoke()相同,但会自动完成。样板代码:

this.BeginInvoke((MethodInvoker)(() => SomeMethod())); // Check files in the Main thread otherwise threading issues occur 
public Form1() {
    InitializeComponent();
    fileSystemWatcher1.SynchronizingObject = this;
}

WPF还是WinForms?WinForms使用事件处理程序中的
Control.Invoke()
,WPF使用事件处理程序中的
Dispatcher.BeginInvoke()
将事件传递给UI线程。WinForms-我的意思是更改事件-不是如何更新GUI您不必更新GUI,但控件知道它们在哪个线程上,这将得到一些在该线程上执行的代码。你做什么取决于你自己。@steve你是对的。你想添加一个答案让我接受吗?按照Hans Passant的答案-我不知道这个解决方案。这是一个简单的问题,很难看出你的问题。谢谢Hans-我想很多人会从你的智慧中受益如果你在设计器中添加FileSystemWatcher,同步对象在代码隐藏文件中自动设置。