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?