Search code examples
c#parallel-processing

How to I use SemaphoreSlim or Dataflow to handle notification throttling?


I have a situation where our application receives notifications when information is updated. When we receives a notification, we will fetch the latest change in async fashion in a WPF application. What I like to do is when a flood of notification comes, it will accumulate and be throttled.

So if there were 10 notifications send to the client, the system will wait 5 seconds and call an async method to refresh. Every 5 seconds, it will request for latest changes 1 instead of requesting 10 times for changes.

Can someone guide me with an example on the best way to handle it?


Solution

  • You could use a DispatcherTimer with 5 seconds Interval. Start the timer whenever you receive a notification, provided that it's not already started. In the Tick event handler stop the timer and refresh the displayed data.