简体   繁体   English

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

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

I am developing wpf application using MVVM Prism.我正在使用 MVVM Prism 开发 wpf 应用程序。 I got four views , MainWindow, ViewB, ViewC and ViewD and all are attached with their respective ViewModel.我有四个视图,MainWindow、ViewB、ViewC 和 ViewD,并且所有视图都附有各自的 ViewModel。 I initialized the backgroundworker at application start.我在应用程序启动时初始化了后台工作器。 see the following background worker 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;
            }
        }
    }

I called it from MainWindow.xaml.cs and start background worker.我从 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();
    }  
}

How can i call the GetPercent variable from the rest of the view?如何从视图的其余部分调用 GetPercent 变量? For example, when i navigate to ViewC, ViewC show the real time value using Label or TextBlock (Value is keep increasing.).例如,当我导航到 ViewC 时,ViewC 使用 Label 或 TextBlock 显示实时值(​​值不断增加。)。 Then i go to ViewB, ViewB also show real time increment of GetPercent variable.然后我转到 ViewB,ViewB 还显示 GetPercent 变量的实时增量。

Any help, really appreciate it.任何帮助,真的很感激。

Thanks谢谢

This is the best scenario to use events and delegates.In your BackGroundThread create a EventHandler and listen to that event in other class.这是使用事件和委托的最佳方案。在您的BackGroundThread创建一个 EventHandler 并在其他类中侦听该事件。

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

        }

    }

}

You can access this value in your other view's viewmodel您可以在其他视图的视图模型中访问此值

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

likewise you can do this in as many classes you want.同样,您可以在任意数量的课程中执行此操作。

Straight from the main BackgroudWorker page on MSDN直接从 MSDN 上的BackgroudWorker主页
You need to make a call to worker.ReportProgress(...);你需要打电话给 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM