C# 后台工作者不会报告进度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/854548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:18:42  来源:igfitidea点击:

Backgroundworker won't report progress

c#wpfbackgroundworkerprogress

提问by Angela

I have a background worker running a long database task. i want to show the progress bar while the task is running. Somehow the background worker won't report the progress of the task.

我有一个后台工作人员运行一个很长的数据库任务。我想在任务运行时显示进度条。不知何故,后台工作人员不会报告任务的进度。

This is what i have:

这就是我所拥有的:

BackgroundWorker _bgwLoadClients;

_bgwLoadClients = new BackgroundWorker();
_bgwLoadClients.WorkerReportsProgress = true;
_bgwLoadClients.DoWork += new DoWorkEventHandler(_bgwLoadClients_DoWork);
_bgwLoadClients.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bgwLoadClients_RunWorkerCompleted);
_bgwLoadClients.ProgressChanged += new ProgressChangedEventHandler(_bgwLoadClients_ProgressChanged);
_bgwLoadClients.RunWorkerAsync(parms);

private void _bgwLoadClients_DoWork(object sender, DoWorkEventArgs e)
{
    DataTable dt = getdate();
    e.Result = dt;
}

void _bgwLoadClients_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

I am doing this in WPF, but i guess it won't make a difference.

我在 WPF 中这样做,但我想它不会有什么不同。

Thanks in advance

提前致谢

采纳答案by Ray Hayes

You need to break your DoWork method down into reportable progress and then call ReportProgress.

您需要将 DoWork 方法分解为可报告的进度,然后调用 ReportProgress。

Take for example the following:

以以下为例:

private void Something_DoWork(object sender, DoWorkEventArgs e) 
{
    // If possible, establish how much there is to do
    int totalSteps = EstablishWorkload();

    for ( int i=0; i<totalSteps; i++)
    {
        // Do something...

        // Report progress, hint: sender is your worker
        (sender as BackgroundWorker).ReportProgress((int)(100/totalSteps)*i, null);
    }

}

If your work can't be predetermined, try adding your own percentages:

如果您的工作无法预先确定,请尝试添加您自己的百分比:

private void Something_DoWork(object sender, DoWorkEventArgs e) 
{
    // some work

    (sender as BackgroundWorker).ReportProgress(25, null);

    // some work

    (sender as BackgroundWorker).ReportProgress(50, null);

    // some work

    (sender as BackgroundWorker).ReportProgress(60, null);

    // some work

    (sender as BackgroundWorker).ReportProgress(99, null);
}

回答by Tolgahan Albayrak

just report progress on dowork event

只需报告 dowork 事件的进度

private void _bgwLoadClients_DoWork(object sender, DoWorkEventArgs e) {
    int progresValue0to100 = 75;
    (sender as System.ComponentModel.BackgroundWorker).ReportProgress(progresValue0to100);
    //do your jobs..
}

it works like this

它是这样工作的

回答by Daniel Brückner

You have to manualy call ReportProgress()to raise the ProgressChangedevent.

您必须手动调用ReportProgress()以引发ProgressChanged事件。

回答by Aleris

You need to call worker.ReportProgress(percentComplete) in your DoWork method. percentComplete should be computed based on the total work. For example:

您需要在 DoWork 方法中调用 worker.ReportProgress(percentComplete)。percentComplete 应根据总工作量计算。例如:

for(int i =0; i != 100; i++) {
    // do something
    worker.ReportProgress(i);
}

Sometimes it is difficult to partition a job in several chunks to be possible to report the progress. Unfortunately the BackgroundWorker does not solve this, you have to do it yourself.

有时很难将一个作业分成几个块来报告进度。不幸的是,BackgroundWorker 没有解决这个问题,你必须自己做。

回答by Adam Says - Reinstate Monica

Progress must be sent from within the DoWork event by calling the ReportProgress method on the BackgroundWorker. In your case, you can't report any progress because all of the work is being done outside of the DoWork function. You can only report progress before and after the call to getdate(), but not during the call since the BackgroundWorker thread is busy.

必须通过调用 BackgroundWorker 上的 ReportProgress 方法从 DoWork 事件内部发送进度。就您而言,您无法报告任何进度,因为所有工作都在 DoWork 功能之外完成。您只能在调用 getdate() 之前和之后报告进度,但不能在调用期间报告进度,因为 BackgroundWorker 线程很忙。

回答by prem

Modify the WorkReportProgressproperty of the backgroundworker object to trueeither in the properties window or in code.

在属性窗口或代码中将 backgroundworker 对象的WorkReportProgress属性修改为true