如何使用Throttle过滤事件

时间:2010-09-17 06:09:01

标签: c# .net system.reactive reactive-programming

我正在以胜利形式尝试反应框架。

IObservable<IEvent<MouseEventArgs>> mouseMoves = Observable.FromEvent<MouseEventArgs>(this, "MouseMove");
IObservable<IEvent<MouseEventArgs>> mouseDowns = Observable.FromEvent<MouseEventArgs>(this, "MouseDown");
IObservable<IEvent<MouseEventArgs>> mouseUps = Observable.FromEvent<MouseEventArgs>(this, "MouseUp");
var pointChanges = mouseMoves.Zip(mouseMoves.Skip(1), (prev, post) => new { Prev = prev, Post = post });
var mouseDrags = from d in mouseDowns
                 from p in pointChanges.TakeUntil(mouseUps)
                 select p;

我想过滤mouseDrags,只需要在MouseDowns需要后超过1秒的MouseUps。怎么样?

1 个答案:

答案 0 :(得分:2)

from d in mouseDowns.Timestamp()
from p in pointChanges
    .TakeUntil(mouseUps)
    .SkipUntil(Observable.Timer(d.Timestamp + TimeSpan.FromSeconds(1.0)))
select p;