[英]Access Method from FileSystemWatcher Event Handler in C#
我試圖使用FileSystemWatcher從事件處理程序內部訪問方法。 似乎已經問過這個問題的變化,但我肯定似乎無法使用它們中的任何一個來回答這一點。 在下面的代碼中,我希望能夠從OnChanged訪問ReadNoteFile但不能。 任何幫助將不勝感激。
public void CreateFileWatcher(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
MessageBox.Show("New note has arrived!");
//run ReadNoteFile here
}
public void ReadNoteFile(string path)
{
//do some stuff
}
FileSystemEventHandler
構造函數需要(object, IntPtr)
參數。
我建議跳過另一個對象的創建並直接傳遞一個lambda(如今在這種情況下的慣常做法):
watcher.Changed += delegate() { MessageBox.Show("New note has arrived!"); };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.