我想创建一个可以同时观察Renamed
和Changed
的Reactive
Observable
。
我正在使用FileSystemWatcher
监视已修改和重命名文件的文件夹。我只想在事件稳定下来后处理事件(因为有时Changed
事件可能会多次引发)。 所以我选择了.netReactive Extension
的Throttle
功能。
我可以通过观察单个Changed
事件来设法完成工作,但无法弄清楚如何在单个Observable
中观察Changed
和Renamed
。
以下是我为单个Changed
事件所做的工作:
var watcher = new FileSystemWatcher();
watcher.Path = "d:\test";
var observable = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
ev => watcher.Changed += ev,
ev => watcher.Changed -= ev
);
var d = observable.Throttle(TimeSpan.FromSeconds(5))
.Subscribe(
a => Console.WriteLine($"Watcher type:{a.EventArgs.ChangeType }, file:{a.EventArgs.Name }, sender: {a.Sender}"),
ex => Console.WriteLine("observer error:" + ex.ToString()),
() => Console.WriteLine("observer complete.")
);
但是当我想观察这两个事件时,VS 告诉我FileSystemEventHandler
和RenamedEventHandler
不兼容(尽管RenamedEventArgs
可以安全地投射到FileSystemEventArgs
):
var observable = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
ev => {
watcher.Changed += ev;
//Error
watcher.Renamed += ev;
},
ev => {
watcher.Changed -= ev;
//Error
watcher.Renamed -= ev;
});
我知道我可以将ev
转换为兼容的委托,但我想知道如何记录该委托并将其从事件中删除?因为我读到匿名委托不能从event
中删除,除非在注册时记录它(如(这个问题)[添加和删除匿名事件处理程序中所述)。也就是说,这样做一点也不优雅...
Func<FileSystemEventHandler, RenamedEventHandler> renameDelegate = (FileSystemEventHandler h) =>
{
return (object sender, RenamedEventArgs args) => h(sender, args);
};
var observable = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
ev => { watcher.Changed += ev;
//registered delegate
watcher.Renamed += renameDelegate(ev);
},
ev => { watcher.Changed -= ev;
//this seems not to be identical with the one registered
watcher.Renamed -= renameDelegate(ev); });
);
那么,如何在同一Observable
中观察Changed
和Renamed
事件呢?
编辑 1
感谢@Enigmativity的回答,我的最终代码如下所示(示例代码):
var o1 = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
ev => watcher.Changed += ev,
ev => watcher.Changed -= ev);
var o2 = Observable.FromEventPattern<RenamedEventHandler, RenamedEventArgs>(
ev => watcher.Renamed += ev,
ev => watcher.Renamed -= ev)
.Select(x => new System.Reactive.EventPattern<FileSystemEventArgs>(x.Sender, x.EventArgs));
var o = Observable.Merge(o1, o2);
var d = o.Throttle(TimeSpan.FromSeconds(5))
.Subscribe(
a => Console.WriteLine($"Watcher type:{a.EventArgs.ChangeType }, file:{a.EventArgs.Name }, sender: {a.Sender}"),
ex => Console.WriteLine("observer error:" + ex.ToString()),
() => Console.WriteLine("observer complete.")
以下是您的操作方法:
var observable =
Observable
.Using(
() => new FileSystemWatcher() { Path = "d:\test" },
watcher =>
Observable
.Merge(
Observable
.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
ev => watcher.Changed += ev,
ev => watcher.Changed -= ev)
.Select(x => new
{
x.EventArgs.Name,
x.EventArgs.FullPath,
x.EventArgs.ChangeType,
OldName = (string)null,
OldFullPath = (string)null,
}),
Observable
.FromEventPattern<RenamedEventHandler, RenamedEventArgs>(
ev => watcher.Renamed += ev,
ev => watcher.Renamed -= ev)
.Select(x => new
{
x.EventArgs.Name,
x.EventArgs.FullPath,
x.EventArgs.ChangeType,
x.EventArgs.OldName,
x.EventArgs.OldFullPath
})));