如何在backgroundworker wpf c# mvvm 获取运行 的进度值

How to get the progress value that running at backgroundworker wpf c# mvvm

我正在使用 MVVM Prism 开发 wpf 应用程序。我有四个视图,MainWindow、ViewB、ViewC 和 ViewD,它们都附有各自的 ViewModel。我在应用程序启动时初始化了 backgroundworker。请参阅以下后台工作人员 class.

 public class BackGroundThread
    {
        public BackgroundWorker bgWrk;
        public bool stopStatus;
        public int GetPercent;

        public BackGroundThread()
        {
            bgWrk = new BackgroundWorker();

            bgWrk.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
            bgWrk.ProgressChanged += backgroundWorker_ProgressChanged;
            bgWrk.DoWork += bw_DoWork;
            bgWrk.WorkerReportsProgress = true;

        }

        public void backgroundWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e) { 

        }

        public void backgroundWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
        {
            Overall.EverythingOk = e.ProgressPercentage.ToString();

        }

        public void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = 1;


            while (!stopStatus)
            {

                GetPercent = I;

                if (i == int.MaxValue)
                    i = 0;
                    i = i + 1;
            }
        }
    }

我从 MainWindow.xaml.cs 调用它并启动后台工作程序。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();            
        ViewModel.MainWindowsViewModel viewModel = new ViewModel.MainWindowsViewModel();
        this.DataContext = viewModel;
        BackGroundThread bgT = new BackGroundThread();
        bgT.bgWrk.RunWorkerAsync();
    }  
}

如何从视图的其余部分调用 GetPercent 变量?例如,当我导航到 ViewC 时,ViewC 使用 Label 或 TextBlock 显示实时值(​​值不断增加。)。然后我转到ViewB,ViewB也显示了GetPercent变量的实时增量。

任何帮助,真的很感激。

谢谢

这是使用事件的最佳方案,delegates.In 您的 BackGroundThread 创建一个 EventHandler 并在其他 class.

中收听该事件
 public class ProgressBarEventArgs:EventArgs 
 {
        public int CompletedPercent{get;set;}
 }

public class BackgroundThread
{
    public static event EventHandler<ProgressBarEventArgs> CompletedEventHandler;

    public void backgroundWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
    {
        Overall.EverythingOk = e.ProgressPercentage.ToString();
        if (CompletedEventHandler != null)
        {
            CompletedEventHandler(sender, new ProgressBarEventArgs { CompletedPercent = e.ProgressPercentage });

        }

    }

}

您可以在其他视图的视图模型中访问此值

    class AnotherviewModel
    {
        BackgroundThread.CompletedEventHandler+=w_ChangedEventHandler;
        void w_ChangedEventHandler(object sender, ProgressBarEventArgs e)
        {
          // get e.Percentage
        }
    }

同样,您可以根据需要在 class 中执行此操作。

直接来自 MSDN BackgroudWorker 主页面
您需要致电 worker.ReportProgress(...);

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

    for (int i = 1; i <= 10; i++)
    {
        if (worker.CancellationPending == true)
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress(i * 10);
        }
    }
}