I want a background worker to continue running and occasionally update the user interface. My first idea was to use a BackgroundWorker and in its completed event just fire it up again.
Is using a BackgroundWorker in this fashion acceptable? Or are there potential issues from using the completed event to trigger the worker?
Below is some Pseudo code of what my intentions are
class Program
{
private static BackgroundWorker worker;
private static Int32 runs = 0;
static void Main(string[] args)
{
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync(runs);
Console.ReadLine();
}
static void worker_DoWork(object sender, DoWorkEventArgs e)
{
//Do time consuming work
Thread.Sleep(3000);
}
static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Update the UI and start time consuming work again
runs++;
Console.WriteLine("Completed run #" + runs);
worker.RunWorkerAsync();
}
}