1

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?

Andrew Wu
  • 13
  • 3
  • If only one notification is received by the client, it should still wait for 5 seconds before refreshing? – Theodor Zoulias Feb 05 '20 at 21:41
  • yes. it will wait 5 seconds before refreshing as notifications comes very rapidly. – Andrew Wu Feb 05 '20 at 21:52
  • Throttling of what? _Receiving_ the notification or _displaying_? I'm guessing displaying (as in Windows Action Centre popups). Any code to show? [mcve] –  Feb 05 '20 at 22:10
  • throttling of handling of notification. so for example, every 5 seconds, we cound get x number of notications. Regarless how many notifications, I will only need make a request to get latest changes once. And in another 5 seconds, i could get 100 notifications that something has changed, I will still only make 1 request to get latest changes on the server side. Thank you. – Andrew Wu Feb 05 '20 at 22:13
  • So basically you are saying you want to process incoming notifications every 5 seconds, where there could be anything from 0 to 100s of notifications waiting. Regardless of scenario, you perform a single check to get the latest. It sounds to me you are `already` doing _"throttling"_. –  Feb 05 '20 at 22:18
  • yes. I am using LimitedConcurrencyLevelTaskScheduler from https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskscheduler?view=netframework-4.8 where I queue up notifications and delays processing by 5 seconds. But I read that TaskScheduler is best for CPU intensive processing. and using semaphoreslim or dataflow is a better way to go but needs some help to how it could be done. Thanks. – Andrew Wu Feb 05 '20 at 22:26
  • There's no need to use that –  Feb 05 '20 at 22:27

1 Answers1

0

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.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104