C# 如何使用 ProgressBar 更新正确实现 BackgroundWorker?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19334583/
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-10 14:46:59  来源:igfitidea点击:

How to correctly implement a BackgroundWorker with ProgressBar updates?

c#wpfmultithreadingbackgroundworker

提问by Ian W

-Updated--14/10also asked this question

-更新--14/10也问了这个问题

To give some clear idea of what is going on and taking into account the comments and from this article here

为了清楚地了解正在发生的事情并考虑到评论和这篇文章here

What I really want to do now is invoke a new form with a progress bar on it and have that run and animate whilst my back ground thread runs my long process to the database and then invoke a close form event

我现在真正想做的是调用一个带有进度条的新表单并运行它并设置动画,同时我的后台线程运行我对数据库的长进程,然后调用一个关闭表单事件

The background worker is set up here

后台工作者设置在这里

 public partial class MainWindow : Window
{
    //Declare background workers
    BackgroundWorker bw = new BackgroundWorker();
    BackgroundWorker bwLoadCSV = new BackgroundWorker();
    BackgroundWorker bwProgressBar = new BackgroundWorker();

Then delegates added here

然后代表在这里添加

  public MainWindow()
    {
        bwLoadCSV.WorkerReportsProgress = true;
        bwLoadCSV.WorkerSupportsCancellation = true;
        bwLoadCSV.DoWork += new DoWorkEventHandler(bwLoadCSV_DoWork);
        bwLoadCSV.ProgressChanged += new ProgressChangedEventHandler(bwLoadCSV_ProgressChanged);
        bwLoadCSV.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoadCSV_RunWorkerCompleted);

The call is made here from the main window class

从主窗口类在此处进行调用

  private void CSV_Load_Click(object sender, RoutedEventArgs e)
    ///Function to read csv into datagrid
    ///
    {
        //Turn Cursor to wait
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

        //Test connection to sql server
        if (CHHoursDataProvider.IsDatabaseOnline() == false)
        {
            System.Windows.Forms.MessageBox.Show("Can not establish contact with sql server" + "\n" + "Contact IT", "Connection Error");
            //Set UI picture
            return;
        }
        //Set a control to update the user here
        tbLoadDgStat.Visibility = Visibility.Visible;

        tbLoadDgStat.Text = "Getting data templete from Database...";
        string FilePath = txFilePath.Text;
        if (bwLoadCSV.IsBusy != true)
        {
            //load the context object with parameters for Background worker
            bwCSVLoadContext Context = new bwCSVLoadContext();
            Context.Site = cBChSite.Text;
            Context.FilePath = txFilePath.Text;
            Context.FileName = fileTest;
            Context.Wageyear = cbWageYear.Text;
            Context.Startdate = ((DateTime)dpStartDate.SelectedDate);
            Context.Enddate = ((DateTime)dpEndDate.SelectedDate);

            bwLoadCSV.RunWorkerAsync(Context);                

        }

The background worker do work is

后台工作者做的工作是

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

        bwCSVLoadContext Context = e.Argument as bwCSVLoadContext;


        worker.ReportProgress((10));
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;

        }
        else
        {
            // Perform a time consuming operation and report progress load csv into datagrid.

To report the background work I do this. This is where I am trying to load a new form call ProgressDialog which has a progress bar on it, which I am try set to Indeterminable so it just "swishes" across my ProgressDialoge form to show the user something is still going on. I have used the reporter part of the background work because I believe it has access to the main window thread and I am hoping that the invoke method is then called from the main window thread, but I am not really sure

为了报告后台工作,我这样做。这是我尝试加载一个新的表单调用 ProgressDialog 的地方,它上面有一个进度条,我尝试将其设置为 Indeterminable,因此它只是在我的 ProgressDialoge 表单上“晃动”以向用户显示某些事情仍在进行中。我使用了后台工作的reporter部分,因为我相信它可以访问主窗口线程,我希望然后从主窗口线程调用invoke方法,但我不太确定

Here is the reporter

这是记者

 private void bwLoadCSV_ProgressChanged(object sender, ProgressChangedEventArgs e)

    {


        this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Visibility = Visibility.Visible; });
        //tbLoadDgStat.Visibility = Visibility.Visible;


        //this.progressBar1.Value = e.ProgressPercentage;//This works but pauses on long steps
        if (e.ProgressPercentage == 10)
        {
            //Try to open a new form with a class ProgressDialog and set the progressbar
            // on the frm to IsIndeterminate=true
            //THIS IS NOT WORKING
            this.Dispatcher.BeginInvoke (new Action(() =>
              {  ProgressDialog progressDialog = new ProgressDialog();
                progressDialog.SetIndeterminate(true);
            }));

             //this updates the main form OK
             this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Text = "Getting data templete from Database..."; });

        }
        else if (e.ProgressPercentage == 20)
        {

            this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Text = "Data template retrieved..."; });

        }
        else
        {

            if (e.ProgressPercentage % 10 == 0)
            {
                this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Text = "Adding Data..." + e.ProgressPercentage.ToString() + "%"; });
            }
        }

Lastly the xaml for the ProgressDialog Form and it's class

最后是 ProgressDialog 表单的 xaml 及其类

<Window x:Class="Test_Read_CSV.ProgressDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Progress Dialog" Height="115" Width="306" Name="ProgressPopup">
<Grid>
    <ProgressBar Height="31" HorizontalAlignment="Left" Margin="12,33,0,0" Name="progressBar1" VerticalAlignment="Top" Width="250" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="7,4,0,0" Name="tbEvent" VerticalAlignment="Top" Width="254" IsReadOnly="True" IsEnabled="False" />
</Grid>

class

班级

/// <summary>
/// Interaction logic for ProgressDialog.xaml
/// </summary>
public partial class ProgressDialog : Window
{
    public ProgressDialog()
    {
        WindowStartupLocation = WindowStartupLocation.CenterScreen;
        InitializeComponent();
    }

    public ProgressDialog(String Purpose)
    {
        InitializeComponent();
        tbEvent.Text = Purpose;
        WindowStartupLocation = WindowStartupLocation.CenterScreen;

    }
    public void UpdateProgress(int progress)
    {
        progressBar1.Dispatcher.BeginInvoke(
            new Action(() =>
            {
                progressBar1.Value = progress;
            }
        ));
    }

    public void SetIndeterminate(bool isIndeterminate)
    {
        progressBar1.Dispatcher.BeginInvoke(
            new Action(() =>
            {
                if (isIndeterminate)
                {
                    progressBar1.IsIndeterminate = true;
                }
                else
                {
                    progressBar1.IsIndeterminate = false;
                }
            }
        ));
    }
}

}

I have read and done a number of tutorial on background worker and even some on threads but can not seem to get the result I want

我已经阅读并完成了许多关于后台工作者的教程,甚至一些关于线程的教程,但似乎无法得到我想要的结果

The idea is I have two long processes where I am either getting a datatable clone from my remote bd or I am updating the db from my wpf application (.net 4). While the process is running I want a progress bar control and to update it, for the obivous reason of making it clear that some work is going on. So I did the usual report progress routines in the background worker and it works....However, in my dowork thread I have this command

这个想法是我有两个很长的过程,我要么从远程 bd 获取数据表克隆,要么从 wpf 应用程序(.net 4)更新数据库。在进程运行时,我想要一个进度条控件并对其进行更新,原因很明显是要明确一些工作正在进行中。所以我在后台工作人员中做了通常的报告进度例程并且它有效......但是,在我的 dowork 线程中我有这个命令

CHHoursDataProvider CH = new CHHoursDataProvider();
oTable = CH.CloneCHHours();

this where the communication with db is and this command takes a good 60 - 90 secs on a vpn remote connection so even if I do this

这是与 db 通信的地方,并且此命令在 vpn 远程连接上需要 60 - 90 秒,所以即使我这样做

CHHoursDataProvider CH = new CHHoursDataProvider();
worker.ReportProgress((10));
oTable = CH.CloneCHHours();
worker.ReportProgress((20));

The main window still looks frozen and like it has crashed!

主窗口看起来仍然冻结,就像它崩溃了一样!

So all I want to do is at the start of the call to the background work is set a progressbar running and leave it running till the end of the task. This is all I need to do to finish my first ever project and after three days I still can not get my head around it!

所以我想要做的就是在对后台工作的调用开始时设置一个正在运行的进度条并让它一直运行到任务结束。这就是我完成我的第一个项目所需要做的一切,三天后我仍然无法理解它!

So I have tried the follow

所以我尝试了以下

In the bw progress changed and in the main window class

在 bw 进度更改和主窗口类中

 this.progressBar2.IsIndeterminate = true;

However the animation does not start till the Dowork thread has finished.

但是动画直到 Dowork 线程完成后才开始。

I then created another background worker to do update the progressbar2, which linked to a button on the main window was ok, but as soon as I tried to use it from the other background worker or from the main window class did not run till the dowork thread had completed on the first background worker

然后我创建了另一个后台工作程序来更新progressbar2,它链接到主窗口上的一个按钮是可以的,但是一旦我尝试从其他后台工作程序或主窗口类中使用它,直到 dowork 才运行线程已在第一个后台工作线程上完成

I then tried to follow a invoke method but REALLY got lost on that!

然后我尝试遵循调用方法,但真的迷失了!

So can anyone help I can guess it is something to do with threading and working on the wrong thread etc but what I do about it I have no clue.

所以任何人都可以帮助我猜测这与线程和在错误的线程上工作等有关,但我对此做了什么我不知道。

I can post more code as needed

我可以根据需要发布更多代码

Ian

伊恩

采纳答案by Sheridan

As you haven't shown your full BackgroundWorkercode, I can't tell if you have implemented it correctly. As such, all I can do is to show you a simple working example of updating a ProgressBarcontrol:

由于您还没有展示完整的BackgroundWorker代码,我无法判断您是否正确实现了它。因此,我所能做的就是向您展示一个更新ProgressBar控件的简单工作示例:

UserControlXAML:

UserControlXAML:

<UserControl x:Class="WpfApplication1.Views.TestView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
    <ProgressBar x:Name="progressBar" Height="25" Margin="20" Minimum="0" 
        Maximum="50" />
</UserControl>

MainWindowXAML:

MainWindowXAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Views="clr-namespace:WpfApplication1.Views"
    Title="MainWindow" Height="350" Width="525">
    <Views:TestView />
</Window>

UserControlcode behind:

UserControl后面的代码:

using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1.Views
{
    public partial class TestView : UserControl
    {
        private BackgroundWorker backgroundWorker = new BackgroundWorker();

        public TestView()
        {
            InitializeComponent();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.ProgressChanged += ProgressChanged;
            backgroundWorker.DoWork += DoWork;
            // not required for this question, but is a helpful event to handle
            backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            backgroundWorker.RunWorkerAsync();
        }

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                // Simulate long running work
                Thread.Sleep(100);
                backgroundWorker.ReportProgress(i);
            }
        }

        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // This is called on the UI thread when ReportProgress method is called
            progressBar.Value = e.ProgressPercentage;
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // This is called on the UI thread when the DoWork method completes
            // so it's a good place to hide busy indicators, or put clean up code
        }
    }
}