Reputation: 9950
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?
Upvotes: 1
Views: 1754
Reputation: 150108
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.
private BackgroundWorker worker = new BackgroundWorker();
public MyForm() // Your form's constructor
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}
private void btnUpload_Click(object sender, EventArgs e)
{
if (!worker.IsBusy) // Don't start it again if already running
{
// Start the asynchronous operation.
// Maybe also disable the button that starts background work (btnUpload)
worker.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
worker.ReportProgress(10);
RunLongProcess();
worker.ReportProgress(20);
RunAnotherLongProcess();
worker.ReportProgress(50);
RunOneMoreLongProcess();
worker.ReportProgress(100);
}
// This event handler updates the progress.
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressbar.value = e.ProgressPercentage;
}
// This event handler deals with the results of the background operation.
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Inform the user that work is complete.
// Maybe re-enable the button that starts the background worker
}
Upvotes: 5