【问题标题】:Backgroundworker doesn't update progress bar in separate windowBackgroundworker 不会在单独的窗口中更新进度条
【发布时间】:2014-08-05 20:33:51
【问题描述】:

我让它在 Indeterminate 模式下工作,但想显示实际进度。

当我尝试从 BackgroundWorker 中更新时,我收到错误消息“调用线程无法访问此对象,因为不同的线程拥有它。”但是,我看到的示例似乎正在做我想做的事情。大多数教程在主窗口中使用进度条,我希望在操作发生时出现一个单独的窗口。这是.Net 4

进度条窗口

<Window x:Class="WpfTest.ProgressBarPopup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ProgressBarPopup" Height="100" Width="300">
    <Grid Margin="20">
        <ProgressBar Minimum="0" Maximum="100" Height="20" x:Name="UpdateProgress" Value="10" IsIndeterminate="False" />
        <!-- {Binding Path=PercentDone}-->
    </Grid>
</Window>

后面的代码:

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for ProgressBarPopup.xaml
    /// </summary>
    public partial class ProgressBarPopup : Window
    {
        public ProgressBarPopup()
        {
            InitializeComponent();
            UpdateProgress.Value = 60;
        }
    }
}

主窗口: (我删除了一些关于 ObservableCollection 的内容以整理代码。)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel; // for background task
using System.Threading;    // for thread.sleep

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {    
        ProgressBarPopup bar = new ProgressBarPopup();

        bool barExists = true;

        public MainWindow()
        {
            InitializeComponent();    
        }

        private void Progress_Click(object sender, RoutedEventArgs e)
        {
            if (!barExists)
            {
                bar = new ProgressBarPopup();
                barExists = false;
            }

            bar.Show();
#if true
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunCompleted);

            worker.RunWorkerAsync();
#else       
            // this simple method of doing work in foreground doesn't work with progress bars
            for (int i = 0; i < 50; i++)
            {
                Thread.Sleep(100);
            }
            bar.Hide();
            bar.Close();  // need to clean up
#endif
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 0; i < 50; i++)
            {
                bar.UpdateProgress.Value = i*2;       //  <<<=== gives Error here 
                Thread.Sleep(100);
            }
        }

        void worker_RunCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bar.Hide();
            bar.Close();
            barExists = false;
        }
    }
}

【问题讨论】:

  • 您可以从非 UI 线程更新 UI 上的某些内容。您需要通过执行类似于此的操作向 UI 线程报告进度:codeproject.com/Tips/83317/…
  • 我之前尝试过,但要么我错过了从 DoWork 内部对 ReportProgress 的调用,要么我使用的示例没有它。我会试试看的。
  • 谢谢,成功了!我讨厌提交可能是重复的问题,但我真的把我的头撞到了一堵砖墙上。再次感谢。

标签: c# wpf backgroundworker


【解决方案1】:

您无法从 DoWork() 方法访问 UI 线程,因为该方法中的代码在单独的线程上运行。您必须为此使用BackgroundWorker.ProgressChanged 事件和BackgroundWorker.ReportProgress 方法...

【讨论】:

    【解决方案2】:

    您将不得不像这样调用控件。表单控件在自己的线程上运行。由于后台工作人员单独运行,它没有足够的权限来访问该控件。请查看 MethodInvoker 以将您的控件与后台工作人员分开。

    void worker_RunCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
               _RunCompleted();
            }
    
    
    private void _RunCompleted(){
    MethodInvoker action = delegate
                {
               bar.Hide();
                bar.Close();
                barExists = false;
    };
    }
    

    【讨论】:

    • 根据其他答案,我认为这通常是正确的方向,但我在处理过程中寻找一些东西,而不是在运行完成时。也许委托操作方法可以适用于此,但我使用了 BackgroundWorker.ReportProgress 方法,这是有效的。谢谢
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多