如何节流事件的速度,而无需使用接收框架 [英] How to throttle the speed of an event without using Rx Framework

查看:119
本文介绍了如何节流事件的速度,而无需使用接收框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要节流事件,我怎么能做到这一点,而无需使用微软接收框架的速度。我曾与接收的帮助下完成这一点。但我想的是,我需要根据一个时隙掐死地图的视图更改事件。是否有可能实现相同而无需使用接收。

I want to throttle the speed of an event, How I can achieve this without using Microsoft Rx framework. I had done this with the help of Rx. But what I am trying is, I need to throttle Map's View changed event based on a time slot. Is it possible to implement the same without using Rx.

我不允许使​​用Rx和我必须保持的二进制尺寸尽可能小。

I am not allowed to use Rx and I have to keep the binary size as small as possible.

推荐答案

这工作,如果你的事件的类型是事件处理程序和LT的; EventArgs的> 为例。它可以为你的事件处理程序的包装是节流:

This works, if your event is of type EventHandler<EventArgs> for example. It creates a wrapper for your event handler that is throttled:

private EventHandler<EventArgs> CreateThrottledEventHandler(
    EventHandler<EventArgs> handler, 
    TimeSpan throttle)
{   
    bool throttling = false;
    return (s,e) =>
    {
        if(throttling) return;              
        handler(s,e);
        throttling = true;
        Task.Delay(throttle).ContinueWith(_ => throttling = false);
    };
}

附加这样的:

this.SomeEvent += CreateThrottledEventHandler(
    (s,e) => Console.WriteLine("I am throttled!"),
    TimeSpan.FromSeconds(5));



虽然,你应该存储处理器从返回 CreateThrottledEventHandler 如果你需要使用来无线化了 - 。= 之后

这篇关于如何节流事件的速度,而无需使用接收框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆