I have used a backgroundworker to set a ProgressBar to show the progress when downloading a file. But the RunWorkerAsync() method of BackgroundWorker doesn't trigger Do_Work event.
/**********Window_Load***********/
BackgroundWorker backgroundWorker1;
private void MainWindow_Load(object sender, RoutedEventArgs e)
{
backgroundWorker1=new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress=true;
backgroundWorker1.DoWork +=new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged +=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
/******Button_Click*******/
void Button_Click(object sender, RoutedEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
/****Events*********/
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("File download complete");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Code to download a file and setting progressbar time.
}
I am new to WPF. Any help appreciated.
Your code looks fine there is no change in code..try to figure out following things
I suggest you can look this article once : Multi-threading with the BackgroundWorker which is doing same thing as you are doing.. Might be your find something missing.