本文介绍了如何等待BackgroundWorker的完成,然后退出控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个示例控制台应用程序中使用的张贴在这里的#1的例子之一,以测试的BackgroundWorker。我有一个BackgroundWorker它的主要方法,但其在操作中,如果我preSS进入,因为我已经写在main方法的console.readkey中间结局开始。但我想这要等到BackgroundWorker的完成做的工作,然后退出应用程序。这是我的code。

 类节目
{
    私有静态BackgroundWorker的工作人员=新的BackgroundWorker();
    私人活动事件处理程序BackgroundWorkFinished;

    静态无效的主要(字串[] args)
    {
        worker.DoWork + = worker_DoWork;
        worker.RunWorkerCompleted + = worker_RunWorkerCompleted;
        worker.ProgressChanged + = worker_ProgressChanged;
        worker.WorkerReportsProgress = TRUE;
        worker.WorkerSupportsCancellation = TRUE;

        Console.WriteLine(启动应用程序...);

        worker.RunWorkerAsync();
        Console.ReadKey();
    }

    静态无效worker_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
    {
        Console.WriteLine(e.ProgressPercentage.ToString());
    }

    静态无效worker_DoWork(对象发件人,DoWorkEventArgs E)
    {
        Console.WriteLine(从现在开始做一些工作......);
        INT I;
        为(ⅰ= 1; I&小于10;我+ +)
        {
            Thread.sleep代码(1000);
            worker.ReportProgress(Convert.ToInt32((100.0 * I)/ 10));
        }

        e.Result =我;
    }

    静态无效worker_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
    {
        Console.WriteLine(我值=+ e.Result.ToString());
        Console.WriteLine(现在完成了......);
    }
}
 

解决方案

见http://stackoverflow.com/questions/123661/net-how-to-wait-for-a-backgroundworker-to-cancel张贴于如何将BackgroundWorker的和你的主线程之间进行通信。

基本上,你必须使用你的DoWork的年底设置来显示DoWork的完成了赛事。然后,WaitOne的()在你的主线程的事件。

I have written a sample console application to test backgroundworker using one of the examples posted here in Stackoverflow. I have a backgroundworker which start with the main method but its ending in the middle of the operation if I press enter because I have written a console.readkey in the main method. But I want it to wait till the backgroundworker has finished doing the job then exit the application. This is my code.

class Program
{
    private static BackgroundWorker worker = new BackgroundWorker();
    private event EventHandler BackgroundWorkFinished;

    static void Main(string[] args)
    {
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;

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

        worker.RunWorkerAsync();
        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;
    }

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

See the http://stackoverflow.com/questions/123661/net-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.

这篇关于如何等待BackgroundWorker的完成,然后退出控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:36