FileSystemWatcher "wrapper" 未触发事件
FileSystemWatcher "wrapper" not triggering events
我已经为 FileSystemWatcher
创建了一个包装器 class,但是我无法触发事件。它们在使用 FileSystemWatcher
class 时按预期触发,但不是我的包装器。
知道为什么吗?
public class FolderWatcher
{
public event FileSystemEventHandler Created;
public event FileSystemEventHandler Changed;
private FileSystemWatcher _watcher;
public FolderWatcher(string watchedFolderPath)
{
_watcher = new FileSystemWatcher(watchedFolderPath);
_watcher.Created += Created;
_watcher.Changed += Changed;
_watcher.EnableRaisingEvents = true;
}
}
class Program
{
static async Task Main(string[] args)
{
string watchedFolder = "/home/user";
// 1. Custom wrapper for FileSystemWatcher. No events get fired.
FolderWatcher watcher = new FolderWatcher(watchedFolder);
// 2. The built in FileSystemWatcher works as expected. Uncomment to test.
// FileSystemWatcher watcher = new FileSystemWatcher(watchedFolder);
// watcher.EnableRaisingEvents = true;
bool createdTriggered = false;
bool changedTriggered = false;
bool wait = (changedTriggered == false && createdTriggered == false);
watcher.Created += (sender, args) =>
{
createdTriggered = true;
Console.WriteLine($"{args.Name} created!");
};
watcher.Changed += (sender, args) =>
{
changedTriggered = true;
Console.WriteLine($"{args.Name} changed!");
};
Console.WriteLine($"Watching folder {watchedFolder}");
while (wait)
{
await Task.Delay(1);
}
Console.WriteLine("Closing application...");
}
}
为了对此进行测试,我刚刚启动它并在 /home/user
中创建一个新的 file/editing 现有文件。场景 1(在代码中注释)不会触发事件,但场景 2(在代码中注释)会触发事件。我正在努力找出原因。
值得注意的是,我正在松散地关注
与其尝试将文件观察器的事件挂接到您的事件,不如在捕获文件观察器的事件时触发您的事件。
watcher.Changed += (sender, args) =>
{
if (Changed != null)
Changed(sender, args);
}
您的活动可供使用您的 class 的人使用。
为了简化 Robert Harvey 的回答,只需将您的构造函数更新为:
public FolderWatcher(string watchedFolderPath)
{
_watcher = new FileSystemWatcher(watchedFolderPath);
_watcher.Created += (sender, args) =>
{
if (this.Created != null)
{
this.Created(sender, args);
}
};
_watcher.Changed += (sender, args) =>
{
if (this.Changed != null)
{
this.Changed(sender, args);
}
};
_watcher.EnableRaisingEvents = true;
}
我已经为 FileSystemWatcher
创建了一个包装器 class,但是我无法触发事件。它们在使用 FileSystemWatcher
class 时按预期触发,但不是我的包装器。
知道为什么吗?
public class FolderWatcher
{
public event FileSystemEventHandler Created;
public event FileSystemEventHandler Changed;
private FileSystemWatcher _watcher;
public FolderWatcher(string watchedFolderPath)
{
_watcher = new FileSystemWatcher(watchedFolderPath);
_watcher.Created += Created;
_watcher.Changed += Changed;
_watcher.EnableRaisingEvents = true;
}
}
class Program
{
static async Task Main(string[] args)
{
string watchedFolder = "/home/user";
// 1. Custom wrapper for FileSystemWatcher. No events get fired.
FolderWatcher watcher = new FolderWatcher(watchedFolder);
// 2. The built in FileSystemWatcher works as expected. Uncomment to test.
// FileSystemWatcher watcher = new FileSystemWatcher(watchedFolder);
// watcher.EnableRaisingEvents = true;
bool createdTriggered = false;
bool changedTriggered = false;
bool wait = (changedTriggered == false && createdTriggered == false);
watcher.Created += (sender, args) =>
{
createdTriggered = true;
Console.WriteLine($"{args.Name} created!");
};
watcher.Changed += (sender, args) =>
{
changedTriggered = true;
Console.WriteLine($"{args.Name} changed!");
};
Console.WriteLine($"Watching folder {watchedFolder}");
while (wait)
{
await Task.Delay(1);
}
Console.WriteLine("Closing application...");
}
}
为了对此进行测试,我刚刚启动它并在 /home/user
中创建一个新的 file/editing 现有文件。场景 1(在代码中注释)不会触发事件,但场景 2(在代码中注释)会触发事件。我正在努力找出原因。
值得注意的是,我正在松散地关注
与其尝试将文件观察器的事件挂接到您的事件,不如在捕获文件观察器的事件时触发您的事件。
watcher.Changed += (sender, args) =>
{
if (Changed != null)
Changed(sender, args);
}
您的活动可供使用您的 class 的人使用。
为了简化 Robert Harvey 的回答,只需将您的构造函数更新为:
public FolderWatcher(string watchedFolderPath)
{
_watcher = new FileSystemWatcher(watchedFolderPath);
_watcher.Created += (sender, args) =>
{
if (this.Created != null)
{
this.Created(sender, args);
}
};
_watcher.Changed += (sender, args) =>
{
if (this.Changed != null)
{
this.Changed(sender, args);
}
};
_watcher.EnableRaisingEvents = true;
}