如何同时多次运行filesystemwatcher的“onchanged”事件?

时间:2016-09-05 13:11:24

标签: c# multithreading winforms filesystemwatcher

我的问题是关于以下内容:

我正在使用FileSystemWatcher,当文件被更改时(机器正在写入文件),它会引发一个方法。我正试图再次提出这个方法,即使前一个方法还没有完成它的工作(我用MessageBox停止了线程,我只是不按OK)。问题是,当文件更改时,OnChanged事件没有再次引发,但前一个线程没有完成。

正如我所知,观看该文件夹正在另一个线程上运行。 ç

public Mainform()
{
   InitializeComponent();
   //....
   fileSystemWatcher1.Changed += new FileSystemEventHandler(start_thread);
   //....
}

当它改变时,运行以下方法,这是我尝试过的,但不起作用。

    private void start_thread(object source, FileSystemEventArgs e)
    {
        Thread thread = new Thread(() => OnChanged(source, e));
        if (System.Diagnostics.Process.GetProcessesByName("thread").Length == 0)
        {
            Thread thread2 = new Thread(() => OnChanged(source, e));
            thread2.Name = "thread2";
            thread2.Start();
        }
        else
        {
            thread.Start();
            thread.Name = "thread";
        }
    }

它引发了OnChanged事件,如下所示:

private void OnChanged(object source, FileSystemEventArgs e)
{
   MessageBox.Show("Something.");
}

0 个答案:

没有答案