I have a method that loops through an IEnumerable
collection and passes each of them to another method. Something like:
void Run (object item)
{
foreach(var method in this.Methods)
method (item);
)
How can I implement sort of progress that will be reflected in a progress bar? I could do this easily if it was directly coded inside this method but this method is contained in a type outside the ViewModel
, which I can call of course.
I just don't know how to implement it and get it from that method and reflect the changes in the UI, by passing it to ViewModel
, etc.
Any ideas?
I would approach this by using events as follows.
What I end up doing is passing in delegate used for reporting progress. This provides very nice decoupling. The delegate can be implemented as a lambda function that directly sets the progress on the form.
For long running tasks, I would do this on a separate thread using BackgroundWorker, which has built in hooks for progress reporting.