Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing wpf application using MVVM Prism. I got four views , MainWindow, ViewB, ViewC and ViewD and all are attached with their respective ViewModel. I initialized the backgroundworker at application start. see the following background worker class.

C#
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.

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? For example, when i navigate to ViewC, ViewC show the real time value using Label or TextBlock (Value is keep increasing.). Then i go to ViewB, ViewB also show real time increment of GetPercent variable.

Any help, really appreciate it.

Thanks
Posted
Comments
VR Karthikeyan 25-Sep-15 2:09am    
Make GetPercent variable as global, Just make a class that holds common properties which are shared among views, also implement INotifyPropertyChanged interface to keep property values synchronized.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900