When you’re designing your application, you need to consider how to handle data loading in order to ensure the application always remains responsive to the user. Generally this is done by loading data asynchronously on a background thread, while the user may continue to interact with the application.

The issue is that without proper visualization, the user may wonder whether anything is happening. This leads to the use of a progress bar to indicate that some activity is being executed in the background.

Once the data has been loaded the progress bar should be hidden and the relevant controls to display the data should be shown.

Show Progress Dialog While Loading Data

Let’s walk through with a few examples working with progress bars in WinForms and WPF.

Indeterminate Progress Bars

The first step is to asynchronously load your data so that the application UI remains responsive during longer-than-usual load times. This is handled pretty easily in .NET using an awaited method call. It means that any code that happens after the await, will not run until the operation is complete. It’s the easiest solution as shown below.

private async Task LoadData()
{
        // show progress bar
        progressBar1.Visible = true;
        await LoadDataAsync();
        // hide progress bar
        progressBar1.Visible = false;
}

All you have to do is set the progress bar visibility before you await the loading of data, and then set it invisible when the operation is complete. This works well if the progress bar is “indeterminate”, which means it will keep looping through the loading animation indefinitely until you stop it.

It’s a common design paradigm today to display an indeterminate progress bar, or ring, as data is loading. This is popular because with unpredictable networks we don’t know exactly how long it may take. The server we are calling may not be capable or willing to inform us how long it will take either.

To create an indeterminate progress bar in WinForms, set its Style property to Marquee. In WPF, set its IsIndeterminate property to True.

#.net #wpf #winforms

Show a Progress Dialog Bar While Loading Data (WinForms and WPF)
28.10 GEEK