难道FileSystemWatcher的创建自己的线程?自己的、线程、FileSystemWatcher

2023-09-02 10:40:04 作者:别海南岸

我希望这项工作在不同的线程来完成,但我一定要创建一个线程,或者它做所有的工作在不同的线程?

像:

 发fileThread =新主题(()=>
{
    FileWatcher =新FileSystemWatcher的();

    FileWatcher.Created + = OnFileEvent;
    FileWatcher.Deleted + = OnFileEvent;
    FileWatcher.Renamed + = OnRenameEvent;
    FileWatcher.EnableRaisingEvents = TRUE;
});

fileThread.Start();
 

解决方案

您不必创建一个线程。该事件将被称为在一个单独的线程自动。

C 的FileSystemWatcher监视没有反应,请问是为什么,代码如下

I want this work to be done in a different thread but do i have to create a thread or does it do all the work on different threads?

Like:

Thread fileThread = new Thread(() =>
{
    FileWatcher = new FileSystemWatcher();

    FileWatcher.Created += OnFileEvent;
    FileWatcher.Deleted += OnFileEvent;
    FileWatcher.Renamed += OnRenameEvent;
    FileWatcher.EnableRaisingEvents = true;
});

fileThread.Start();

解决方案

You don't have to create a thread. The events will be called on a separate thread automatically.