views:

555

answers:

3

I am trying to update my progressbar with the backgroundworker. Only this aint working. Here's my code:

private BackgroundWorker _worker;

public Form1(string[] args)
{
    InitializeComponent();

    // Backgroundworker to update the progressbar
    _worker = new BackgroundWorker();
    _worker.WorkerReportsProgress = true;
    _worker.ProgressChanged += worker_ProgressChanged;
    _worker.DoWork += worker_DoWork;
    _worker.RunWorkerCompleted += worker_WorkCompleted;

}

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    SendItems();
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}
private void worker_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    _running = false;
    HandleGui();
}

private bool SendItems()
{
    try
    {
        GetEvaluationDocumentsToSend();
        _worker.ReportProgress(16);

        GetModifiedEvaluationDocumentsToSend();
        _worker.ReportProgress(32);

        GetTasksToSend();
        _worker.ReportProgress(48);

        GetPostToSend();
        _worker.ReportProgress(64);

        GetContractDocumentsToSend();
        _worker.ReportProgress(80);

        GetModifiedContractDocumentsToSend();
        _worker.ReportProgress(100);

        return true;
    }
    catch (Exception e)
    {
        Log.WriteLog(e.ToString());

        MessageBox.Show(
            "The following error occured while sending the items: \r\n" + e.ToString(),
            "Error",
            MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation);

        return false;
    }
}

private void btnUpdate_Click(object sender, EventArgs e)
{
    _worker.RunWorkerAsync();
}

I don't get any errors, but my progressbar will not update. What am I doing wrong?

+3  A: 

You didn't start the BackgroundWorker !

Add this line after event subscriptions :

_worker.RunWorkerAsync();

EDIT : Now the code shows that you call the RunWorkerAsync()

Try to add Thread.Sleep(1000) after each progress to see if your process is too fast...

Guillaume
I'm sorry, I forgot to mention that in my code. I will update my startpost.
Martijn
A: 

try adding a Invoke/BeingInvoke call around your progressbar update:

this.BeginInvoke((MethodInvoker)delegate
{
    progressBar1.Value = e.ProgressPercentage;
});

This is usually the reason UI's are not getting updated.

Alastair Pitts
The entire point of the background worker is that it takes care of that.
configurator
CAUTION Always call EndInvoke after your asynchronous call completes.http://msdn.microsoft.com/en-us/library/2e08f6yc%28VS.71%29.aspx
Petar Petrov
Fair point. I would still do it as a matter of habit. In this case, I can't work out why his code doesn't work, so I'm just providing a posibility.
Alastair Pitts
How do I use your code?
Martijn
+3  A: 

Well it works on my machine. What does GetEvaluationDocumentsToSend() do? Perhaps it is taking a lot longer than the other methods so it looks like no progress is made because all of the progress is almost instantaneous? Also, what does HandleGui() do and what is _running used for?

configurator