Report progress in a task in C sharp

One of my first questions starting programming in Visual Studio WPF was how to run processes without freezing the GUI.

After learning the basic concepts of backgroundworkers and tasks in c sharp programming I wanted to know how to report task progress while the task was not yet finished.

I found all kind of solutions to update a progress bar. I also found solutions to use the result for a specific task.

Think still missing for me was to get a screen to show task progress, with detailed logging while the task was still running. In the end this article helped me finding a solution: https://social.technet.microsoft.com/wiki/contents/articles/19020.progress-of-a-task-in-c.aspx

To implement this logic in a solution I needed I created a form and added a button and a listbox.

The button got the following implementation:

private async void ButtonAsyncFileProcessing_Click(object sender, RoutedEventArgs e)
{
var progressIndicator = new Progress(ReportProgress);
await MyMethodAsync(progressIndicator);
}

For the progress indicator I added the following class:

public class MyTaskProgress
{
//current progress
public int CurrentProgressAmount { get; set; }
//total progress
public int TotalProgressAmount { get; set; }
//some message to pass to the UI of current progress
public List CurrentProgressLogging { get; set; }
}

The async method I used for testing was this:

async Task MyMethodAsync(IProgress progress)
{
int sleepTime = 1000;
int totalAmount = 10000;
List log = new List();

for (int i = 0; i <= totalAmount;)
{
await Task.Delay(sleepTime);
log.Add(string.Format("On {0} Message", i));
progress.Report(new MyTaskProgress
{
CurrentProgressAmount = i, TotalProgressAmount = totalAmount, CurrentProgressLogging = log });
i = i + sleepTime;
}
}

The ReportProgress method used in the button click progressIndicator:

private void ReportProgress(MyTaskProgress progress)
{
UpdateScreen(progress.CurrentProgressLogging,
string.Format("{0} out of {1}",
progress.CurrentProgressAmount,
progress.TotalProgressAmount));
}

And this last method calls a UpdateScreen method:

private void UpdateScreen(List info, string status)
{
lbOutput.Items.Clear();
foreach (string s in info)
{
lbOutput.Items.Add(s);
}
buttonAsyncFileProcessing.Content = "AsyncFileProcessing" +
"\n" + status;
}

The article that helped me understanding Tasks and backgroundworkers is: https://blog.stephencleary.com/2013/05/taskrun-vs-backgroundworker-intro.html