简体   繁体   English

在 BackgroundWorker doWork 事件中使用 Console.WriteLine

[英]Using Console.WriteLine in a BackgroundWorker doWork event

I have a console application that utilizes the BackgroundWorker object and wanted to test my output with a Console.WriteLine(fooBar).我有一个控制台应用程序,它利用 BackgroundWorker object 并想用 Console.WriteLine(fooBar) 测试我的 output。 However, that application exits as the application executes the Console.WriteLine command.但是,该应用程序会在应用程序执行 Console.WriteLine 命令时退出。

Here's a watered down version that illustrates what I wanted to do:这是一个淡化的版本,说明了我想做的事情:

protected static void bWorker_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = startId; i <= endId; i++)
    {
        Console.WriteLine(i.ToString());
        Thread.Sleep(1000);
    }
}

Any ideas why the application would appear to exit like that?任何想法为什么应用程序会出现这样的退出?

The application exits because it in fact completes execution and has no more work to do;-) Once you schedule the background worker and kick off the thread to do it's thing, you have to tell the main thread to stop and wait for one thing or another.应用程序退出,因为它实际上完成了执行并且没有更多工作要做;-) 一旦你安排后台工作人员并启动线程来做这件事,你必须告诉主线程停止并等待一件事或其他。 A very common way of doing this (Generally used in test/sample code) is to simply issue a Console.ReadKey();执行此操作的一种非常常见的方法(通常用于测试/示例代码)是简单地发出Console.ReadKey(); as the very last line of code in your main method.作为 main 方法中的最后一行代码。 This will cause your application to wait until you press a key before exiting the process.这将导致您的应用程序等到您按下某个键后再退出该过程。

For your backgroundworker set WorkerReportsProgress to true .对于您的backgroundworker工作人员,将WorkerReportsProgress设置为true Subscribe to ProgressChanged event like this:像这样订阅ProgressChanged事件:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (var i = 0; i < 1000; i++)
    {
        backgroundWorker1.ReportProgress(i);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    Console.WriteLine(e.ProgressPercentage);
}

If you need to transfer more than just int from your background thread to UI thread, then you could do something like this:如果您需要的不仅仅是 int 从后台线程传输到 UI 线程,那么您可以执行以下操作:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (var i = 0; i < 1000; i++)
    {
        var myObjectInstance = new MyObject{ ...};
        backgroundWorker1.ReportProgress(null, myObjectInstance);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    var myObjectInstance = (MyObject)e.UserState;
    Console.WriteLine(myObjectInstance);
}

I might not be understanding your setup correctly.我可能没有正确理解您的设置。 I'm guessing that you're running the background thread, but the main process is exiting which causes the thread to be stopped before it gets to do anything.我猜你正在运行后台线程,但是主进程正在退出,这导致线程在它开始做任何事情之前就停止了。 Maybe try putting something in your main process that prevents the main thread from exiting like Console.ReadKey();也许尝试在你的主进程中放入一些东西来阻止主线程退出,比如 Console.ReadKey();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM