By default Task doesn’t report it’s progress as a BackgroundWorker does. But that doesn’t mean we can’t get a progress of a Task. There is a new interface which was introduced with .NET framework 4.5 which is IProgress<T>. This interface exposes a Report(T) method, which the async task calls to report progress.
Let’s go by an example. I have the following async method.
It takes a progress parameter which is a IProgress of type “MyTaskProgressReport” and here is my “MyTaskProgressReport” class.To simulate a time taking task, inside my async method, I have a For loop and inside it I have a Task Delay. In every iteration, progress is reported to the caller. Now let’ see how UI captures this.
On the UI thread, we have to define an event handler Action<T>, which will be called when IProgress<T>.Report is invoked.Now I am going to call my async method. I have created a Progress<T> instance and invoked the async method, which is triggered by a button click.
Here is the output,