本文介绍了进度条和运行方式的背景中的BackgroundWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中创建一个BackgroundWorker的过程。

I create a backgroundworker process in the form..

 BackgroundWorker bw = new BackgroundWorker
        {
            WorkerReportsProgress = true,
            WorkerSupportsCancellation = true
        };  

另外,我有表内的进度条控件(进度条的最小值和最大值是1和100)。

Also, i have a progress bar control inside the form(progress bar minimum and maximum values are 1 and 100).

在我按一下按钮,

在这里的单击事件将触发实际的方法调用,这是为了在后台并行运行,开机有显示进度条的步骤...

where the click event will fire the actual method calls which are supposed to run in the background and in parallel, start showing the progress bar steps....

if (bw.IsBusy)
 {
    return;
 }

 bw.DoWork += (bwSender, bwArg) =>
 {
   MethodCall1(); - Does some database insertions..
   MethodCall2(); - Does some database select...
 }

bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

bw.RunWorkerCompleted += (bwSender, bwArg) =>
{

  bw.Dispose();                  
};

bw.RunWorkerAsync();

有关进度条显示进度...

For progress bar to show the progressing...

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   progressBar.Value = e.ProgressPercentage;
}

我所面临的问题是,进度条是不是踩着,就固步自封,没有进度条的进度。

The issue i face is, progress bar is not stepping, it stands still and there is no progress bar's progress.

我也试过在我的实际方法插入bw.ReportProgress(integervalue),但是,它并没有帮助。

I also tried inserting bw.ReportProgress(integervalue) in my actual methods, but, it didn't help.

我怎么能有一个BackgroundWorker线程处理方法调用和进度条并行显示进度,方法调用完成后,我想结束进度条的一步。

How can i have a backgroundworker thread handling method calls and in parallel display the progress of progress bar, after the method calls are done, i want to end the progress bar step.

推荐答案

由于其他人的评论说,你应该将接线了事件到窗体构造函数或初始化方法。如果将它们添加到BackgroundWorker的每按一下按钮,那么这些方法将在第二次点击第三次点击,等执行两次,三次。

As the others stated in the comments, you should move the wiring up of the events to the form constructor or to an initialization method. If you add them to BackgroundWorker on every button click, then the methods will be executed twice on the second click, three times on the third click, etc.

尝试把你的ReportProgress调用你的DoWork code的方法调用之后。正如汉斯如上所述,除非你有你的方法调用花费的时间相对于彼此一个非常不错的主意,这不会真的是进步的一个准确的重新presentation。

Try putting your ReportProgress calls right after your method calls in your DoWork code. As Hans stated above, unless you have a really good idea of how long your method calls take relative to each other, this will not really be an accurate representation of progress.

对于处置,BackgroundWorker的继承组件,它实现了IDisposable。然而,BackgroundWorker的不覆盖基本实现实际处置过这么称它只是删除它的组件集合。它并不需要显式调用

As for the Dispose, BackgroundWorker inherits from Component, which implements IDisposable. However, BackgroundWorker doesn't override the base implementation to actually dispose of anything so calling it just removes it from the components collection. It doesn't need to be called explicitly.

尝试是这样的:

public partial class Form1 : Form
{
    BackgroundWorker bw = new BackgroundWorker
    {
        WorkerReportsProgress = true,
        WorkerSupportsCancellation = true
    };  

    public Form1()
    {
        InitializeComponent();

        bw.DoWork += (bwSender, bwArg) =>
        {
            //MethodCall1(); - Does some database insertions..
            bw.ReportProgress(50);
            //MethodCall2(); - Does some database select...
            bw.ReportProgress(100);
        };

        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

        bw.RunWorkerCompleted += (bwSender, bwArg) =>
        {

        };
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (bw.IsBusy)
        {
            return;
        }

        bw.RunWorkerAsync();
    }

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

这篇关于进度条和运行方式的背景中的BackgroundWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:44