I currently have a BackgroundWorker (BGW) that I use to output a series of reports. To do this I call RunWorkerAsync and pass through a couple of required parameters.
My question is, how can I raise the BGW's OnProgressChanged event from within the code running on the separate thread? My program outputs 18 reports, some of which take a while to execute and I'd like to update my UI with regular progress updates. Is this even the correct way of doing this?
I think you are looking for ReportProgress i made up a quick example (console application)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.WorkerReportsProgress = true;
bw.RunWorkerAsync();
Console.ReadKey();
}
static void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine("Percentage is:{0}%", e.ProgressPercentage);
}
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
var self = (sender as BackgroundWorker);
self.ReportProgress(10);
Thread.Sleep(1000);
self.ReportProgress(20);
Thread.Sleep(1000);
self.ReportProgress(30);
}
}
}
hope that covers your question.