您可以通过缓冲结果并将它们传递给此方法来完成此操作。基本上,通过将此方法包装在聚合器方法中,您可以防止它一直触发。
//... Class Impl
private string keys = String.Empty;
private readonly TimeSpan bufferDelay = new TimeSpan(100000);
private DateTime lastKeyPress = DateTime.UtcNow;
private void DoSomething(string text)
{
//do something to process text.
keys = String.Empty;
}
public void DoSomethingBuffer(string text)
{
keys =+ text;
var now = DateTime.UtcNow;
if((now-lastKeyPress) > bufferDelay)
{
DoSomething(keys);
}
lastKeyPress = now;
}
//... More Class Impl
您需要使用聚合时间窗口来找到感觉自然的东西,但它应该相对简单。您将直接调用 DoSOthingBuffer 而不是 DoSomethign。