Class DebounceDispatcher
Provides Debounce() and Throttle() methods. Use these methods to ensure that events aren't handled too frequently.
public class DebounceDispatcher
- Inheritance
-
DebounceDispatcher
- Inherited Members
- Extension Methods
Remarks
Methods
Debounce(int, Action<object>, object, DispatcherPriority, Dispatcher)
Debounce an event by resetting the event timeout every time the event is fired. The behavior is that the Action passed is fired only after events stop firing for the given timeout period.
Use Debounce when you want events to fire only after events stop firing after the given interval timeout period.
Wrap the logic you would normally use in your event code into the Action you pass to this method to debounce the event. Example: https://gist.github.com/RickStrahl/0519b678f3294e27891f4d4f0608519a.
public void Debounce(int interval, Action<object> action, object param = null, DispatcherPriority priority = DispatcherPriority.ApplicationIdle, Dispatcher disp = null)
Parameters
interval
intTimeout in Milliseconds.
action
Action<object>Action.
param
objectoptional parameter.
priority
DispatcherPriorityoptional priorty for the dispatcher.
disp
Dispatcheroptional dispatcher. If not passed or null CurrentDispatcher is used.
Throttle(int, Action<object>, object, DispatcherPriority, Dispatcher)
This method throttles events by allowing only 1 event to fire for the given timeout period. Only the first event fired, while interval is running, is handled - all others are ignored. Throttle will fire events every timeout ms even if additional events are pending.
Use Throttle where you need to ensure that events fire at given intervals.
public void Throttle(int interval, Action<object> action, object param = null, DispatcherPriority priority = DispatcherPriority.ApplicationIdle, Dispatcher disp = null)
Parameters
interval
intTimeout in Milliseconds.
action
Action<object>Action .
param
objectoptional parameter.
priority
DispatcherPriorityoptional priority for the dispatcher.
disp
Dispatcheroptional dispatcher. If not passed or null CurrentDispatcher is used.