我正在使用文件觀察器。當一個文件被創建時,文件監視器應該停止,直到它再次被調用。我有以下代碼正在觀看,循環不停止。退出文件觀察器創建文件時
public void startwatch()
{
string path = "C:\\testwatcher\\";
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = false;
watcher.Filter = "*.*";
watcher.Created += watcher_Created;
while (true) ;
}
static void watcher_Created(object sender, FileSystemEventArgs e)
{
string FileName = ("C:\\testfilescrated\\");
string text = File.ReadAllText(e.FullPath);
File.WriteAllText(FileName, text);
}
我想要做的是當while循環應該在創建文件時結束。
非常糟糕。你爲什麼忙着等待可能/不可能發生的事件?異步執行。 – 2014-10-01 09:09:47
@SriramSakthivel如何異步執行 – user3202862 2014-10-01 09:23:45
刪除while true,讓線程繼續。然後處理觀察者的'Created'事件並繼續。提供的信息我無法提供更具體的答案。 – 2014-10-01 09:27:29