Backgroundworker不更新单独窗口中的进度条

本文关键字:窗口 更新 单独 Backgroundworker | 更新日期: 2023-09-27 18:16:44

我得到了这个工作在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;
        }
    }
}

Backgroundworker不更新单独窗口中的进度条

你不能从DoWork()方法访问UI线程,因为该方法中的代码在一个单独的线程上运行。你必须使用BackgroundWorker。ProgressChanged事件和后台工作者。

您必须像这样调用控件。窗体控件在自己的线程上运行。由于后台工作线程是单独运行的,所以它没有足够的权限来访问该控件。请使用MethodInvoker将你的控件与后台worker分开。

void worker_RunCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           _RunCompleted();
        }

private void _RunCompleted(){
MethodInvoker action = delegate
            {
           bar.Hide();
            bar.Close();
            barExists = false;
};
}