How to wait for BackgroundWorker to finish and then exit console application

喜欢而已 提交于 2019-11-30 11:33:47
Andreas Paulsson

See the How to wait for a BackgroundWorker to cancel? post for how to communicate between your BackgroundWorker and your main thread.

Basically, you have to use a event that you set at the end of DoWork to signal that DoWork has completed. You then WaitOne() on that event in your main thread.

The main purpose of a Bgw is to interact with the Windows MessageQueue. In other words it is most useful in WinForms and WPF applications.

A Console application is not the right place to use or test a Bgw. You'll get strange results. Print ManagedThreadId at the key points to see what happens.

And some standard advice: Your worker_RunWorkerCompleted() should check e.Error. Right now it is the same as having an empty catch{} block.
Any error from DoWork will now be thrown when you read e.Result, more complex to handle.

Soham Dasgupta

This is what I have done now. But the console.readkey() is not working. The application is not waiting for the ReadKey() function.

class Program
{
    private static BackgroundWorker worker = new System.ComponentModel.BackgroundWorker();
    private static AutoResetEvent resetEvent = new AutoResetEvent(false);
    static void Main(string[] args)
    {
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.WorkerReportsProgress = true;

        Console.WriteLine("Starting Application...");

        worker.RunWorkerAsync();
        resetEvent.WaitOne();

        Console.ReadKey();
    }

    static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine(e.ProgressPercentage.ToString());
    }

    static void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Console.WriteLine("Starting to do some work now...");
        int i;
        for (i = 1; i < 10; i++)
        {
            Thread.Sleep(1000);
            worker.ReportProgress(Convert.ToInt32((100.0 * i) / 10));
        }

        e.Result = i;

        resetEvent.Set();
    }

    static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("Value Of i = " + e.Result.ToString());
        Console.WriteLine("Done now...");
    }

}

Fixing Edit: Moved resetEvent.Set() to inside DoWork rather than in RunWorkerCompleted. The Completed event handler will never get called because the main thread is waiting for the event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!