1
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?

JJ.
  • 9,580
  • 37
  • 116
  • 189
  • You need to do the processing off of the UI thread with a BackgroundWorker or something similar – Joe Aug 27 '12 at 18:30

1 Answers1

5

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
}
Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Thanks for the tip Eric. I'm on 3.5 framework so I guess I'll be using BackgroundWorker. So you're saying for the long processes, I should use backgroundworker and for the main thread, do the progress bar updating. Is this correct? – JJ. Aug 27 '12 at 18:48
  • BackgroundWorker is a very convenient way to handle long-running processes. One nice thing about BackgroundWorker is that it offers a ProgressChanged event. Have your background worker call ReportProgress as needed, and handle the ProgressChanged event in your UI code. That event already fires on your UI thread, so there is no need to do any marshalling. – Eric J. Aug 27 '12 at 18:59
  • Would you be able to give me an example on this? I'm new to this and from the articles I'm reading online it's just confusing me even more. Would you be able to give me an example given my original post? – JJ. Aug 27 '12 at 19:03