帮助C#.NET BackgroundWorker

时间:2011-08-22 14:37:01

标签: backgroundworker

我正在尝试在我的应用中使用BackgroundWorker进行非常简单的测试。我可以看到后台工作者正在做这件事,但由于某种原因,主线程正在获取任何进度通知。

我必须遗漏一些非常基本的东西。有谁可以指出我哪里出错?

这是我的简单测试的代码:

namespace TestBackgroundWorker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            progressBar1.Maximum = 100;
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            int progress = 0;
            while (true)
            {
                Thread.Sleep(1000);
                System.Diagnostics.Debug.WriteLine("worker: progress={0}", progress);
                worker.ReportProgress(progress);
                progress = (progress + 10) % 100;
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("main: process progress={0}", e.ProgressPercentage);
            progressBar1.Value = e.ProgressPercentage;
        }
    }
}

然后这是我在输出窗口上得到的内容。它表明工人正在醒来并做其事情,但主线程没有得到任何进展更新:

worker: progress=0
worker: progress=10
worker: progress=20
worker: progress=30
worker: progress=40
worker: progress=50
worker: progress=60
worker: progress=70
worker: progress=80
worker: progress=90
worker: progress=0
worker: progress=10
worker: progress=20
worker: progress=30
worker: progress=40
worker: progress=50
worker: progress=60
worker: progress=70
worker: progress=80
The thread '<No Name>' (0x1e84) has exited with code 0 (0x0).
worker: progress=90
worker: progress=0
worker: progress=10
worker: progress=20
worker: progress=30
worker: progress=40
worker: progress=50
worker: progress=60
worker: progress=70
worker: progress=80
worker: progress=90
worker: progress=0
worker: progress=10
worker: progress=20
worker: progress=30
worker: progress=40
worker: progress=50
worker: progress=60
worker: progress=70
worker: progress=80
worker: progress=90
worker: progress=0
worker: progress=10
worker: progress=20
worker: progress=30
worker: progress=40
worker: progress=50
The thread 'vshost.RunParkingWindow' (0x2760) has exited with code 0 (0x0).
The thread '<No Name>' (0x29c8) has exited with code 0 (0x0).
The program '[3528] TestBackgroundWorker.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

3 个答案:

答案 0 :(得分:1)

您的代码中缺少对ProgressChanged事件的订阅:

backgroundWorker.ProgressChanged + = backgroundWorker ProgressChanged;

答案 1 :(得分:1)

这可能听起来像一个愚蠢的问题,但你确定回调函数绑定到ProgressChanged事件吗?

以下代码:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            new Program().Run();
        }

        void Run()
        {
            var worker = new BackgroundWorker { WorkerReportsProgress = true };

            worker.ProgressChanged += WorkerOnProgressChanged;
            worker.DoWork += WorkerOnDoWork;
            worker.RunWorkerAsync();

            Console.ReadKey();
        }

        private void WorkerOnDoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; ; i++)
            {
                Debug.WriteLine("[Thread #{0,2} (worker)] Progress:{1}", Thread.CurrentThread.ManagedThreadId, i);
                (sender as BackgroundWorker).ReportProgress(i);
                Thread.Sleep(1000);
            }
        }

        private void WorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Debug.WriteLine("[Thread #{0,2} (main  )] Progress:{1}", Thread.CurrentThread.ManagedThreadId, e.ProgressPercentage);
        }
    }
}

将输出以下内容:

[Thread # 7 (worker)] Progress:0
[Thread #11 (main  )] Progress:0
[Thread # 7 (worker)] Progress:1
[Thread #11 (main  )] Progress:1
[Thread # 7 (worker)] Progress:2
[Thread #11 (main  )] Progress:2
[Thread # 7 (worker)] Progress:3
[Thread #11 (main  )] Progress:3
[Thread # 7 (worker)] Progress:4
[Thread #11 (main  )] Progress:4
[Thread # 7 (worker)] Progress:5
[Thread #11 (main  )] Progress:5
[Thread # 7 (worker)] Progress:6
[Thread #11 (main  )] Progress:6
[Thread # 7 (worker)] Progress:7
[Thread #11 (main  )] Progress:7
[Thread # 7 (worker)] Progress:8
[Thread #12 (main  )] Progress:8

答案 2 :(得分:0)

你错过了某个地方的EventHandler作业吗?我在这里看不到一个。

我打赌你错过了一些如下所示的设计师代码:

this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);

这是使用拖放GUI自动生成代码的问题。当它被淹没时,人们不知道如何解决它。

尝试删除BackgroundWorker并重新开始。