Back to Homepage

Async Task with Progress Report and Cancellation

Feng Jiang
Last Updated: 2020-12-06

When building a WinForms application, I encountered a problem where I try to run a long calculation. Because the process can take very long, I want to popup a new form which has a progress bar and a cancel button.

Both the progress report and the cancellation are frequent questions on stackoverflow, but all the answers only provide code snippets without showing the whole picture. By putting several pieces together, I created a complete example to demonstrate a simple solution. It utilizes IReport for progress report and CancellationToken for cancellation.

The example WinForms application has a main form which has three buttons to start the Fibonacci calculation with different setup. When you click one of the buttons, the async calculation will start and a new form will pop up to show progress or let the user cancel the calculation.

If the third button is clicked, both the progress report and the cancellation button will be seen on the new window. Below is the screenshot when the third button is clicked.

The source code can be accessed on GitHub.

© 2020