Search code examples
c#wpfsystem.reactivebufferingtpl-dataflow

Progress<T> buffering for WPF updates


I have a method that takes Progress<T> as a parameter and internally reports progress on it.

Because the progress could happen rapidly (e.g. hundreds of times per second) on a worker thread, I need to buffer it and then update the view-model with the records received, e.g. every half a second.

In the past I've used Observable.FromEventPattern().Buffer(TimeSpan) so I see that it's possible to utilize the same Rx mechanism if I wrap the Progress<T> reporting into an event. However that would seem like an overkill. Is there a more elegant solution?

I've also looked at TPL Dataflow's BufferBlock but I'm not sure if it supports time-based buffering, e.g. every half a second.

If anyone has an example, please post it. It'll be greatly appreciated.


Solution

  • RX is the obvious choice for doing this however you don't "need" to use events at all to use RX, in your case you could simply use a Subject<Progress<T>> and have your method (that currently gets the Progress<T> as a parameter) push the data into the subject. Then you'd have your RX query on the subject just like your previous did on your event sourced observable

    Subject<T> on MSDN

    However if you feel better about doing it with an event the way you're used to, i see nothing overkill about it at all.