How to throttle the speed of an event without using Rx Framework
c#, c#-5.0, system.reactive, windows-8, windows-runtime
Solution
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);
};
}
Attach like this:
this.SomeEvent += CreateThrottledEventHandler(
(s,e) => Console.WriteLine("I am throttled!"),
TimeSpan.FromSeconds(5));
Although, you should store the handler returned from `CreateThrottledEventHandler` if you need to unwire it with `-=` later.
Problem
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. I am not allowed to use Rx and I have to keep the binary size as small as possible.