private void btnUpload_Click(object sender, EventArgs e)
{
progressbar.value = 10;
RunLongProcess();
progressbar.value = 20;
RunAnotherLongProcess();
progressbar.value = 50;
RunOneMoreLongProcess();
progressbar.value = 100;
}
The problem with the above code is that the application is freezing and I can’t see the progress bar functioning correctly.
What’s the correct way to deal with this scenario? I’m not sure why this happens considering I’m not trying to run 2 things at the same time. It’s one thing at a time. Do I need to refresh the app or something like that?
Things are freezing up precisely because you are not “running multiple things at once”.
You are doing a long-running action in the event handler for a button click. Until that event handler returns, the UI is blocked.
Try putting your long running processes in another thread, e.g. using a Task or BackgroundWorker.
The other thread can update the progress bar. However, keep in mind that the separate thread needs to properly access the UI thread. The exact mechanism for that depends on if you are talking about WinForms, WPF or something else (not specified in your question).
This is my favorite approach for updating a control from a non-UI thread for WinForms:
https://stackoverflow.com/a/3588137/141172
UPDATE
Here’s an example. I don’t have an IDE handy so there may be minor issues.