簡體   English   中英

Backgroundworker 不單獨更新進度條 window

[英]Backgroundworker doesn't update progress bar in separate window

我讓它在不確定模式下工作,但想顯示實際進度。

當我嘗試從 BackgroundWorker 中更新時,出現錯誤“調用線程無法訪問此 object,因為另一個線程擁有它。” 但是,我看到的示例似乎正在做我想做的事情。 大多數教程在主 window 中使用進度條,我想要一個單獨的 window 在操作發生時出現。 這是.Net 4

進度條 Window

<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;
        }
    }
}

Main window:(我刪除了一些關於 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;
        }
    }
}

您無法從DoWork()方法訪問UI線程,因為該方法中的代碼在單獨的線程上運行。 您必須為此使用BackgroundWorker.ProgressChanged事件和BackgroundWorker.ReportProgress方法。

您將必須像這樣調用控件。 表單控件在自己的線程上運行。 由於后台工作程序是單獨運行的,因此它沒有足夠的特權來訪問該控件。 請查看MethodInvoker,以將您的控件與后台工作人員分開。

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


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

只有后台工作程序的 Progress Changed 是線程安全的,因此您應該從進度更改對 UI 進行任何更改,但不僅限於 UI。

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++)
            {
                worker.reportprogress(i)
                Thread.Sleep(100);
            }
        }

       void worker_ProgressChanged(object sender, DoWorkEventArgs e)

{
    int i=e.progresschanged
     bar.UpdateProgress.Value = i*2;       //  <<<=== gives Error here 
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM