简体   繁体   中英

wpf c# progress bar wont update untill after work is done when in background worker

below is some code i am using to show a progress bar on window1. However when work is done on my main window the progress bar freezes then jumps to what ever stage the background worker is in. Is there anyway to have the progress bar constantly update. Even if work is being done in other windows

public SplashScreen()
        {
            InitializeComponent();
            //Timer();


            CreateBackGroundWorker();
           // int shareProgressBarValue = MainSharePage.shareProgressBarValue;

            LoginPage LoginPage = new LoginPage();
            LoginPage.Show();


        }

        private void CreateBackGroundWorker()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.WorkerReportsProgress = true;
            worker.DoWork += worker_DoWork;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerAsync();
        }

        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
           this.ProgressBar.Value = e.ProgressPercentage;
            //ShareProgressBarText.Text = (string)e.UserState;
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            worker.ReportProgress(0, String.Format("Processing Iteration 1."));
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(1000);
                worker.ReportProgress((i + 1), String.Format("Processing Iteration {0}.", i + 2));
            }

            worker.ReportProgress(100, "Done Processing.");
        }

I copy-pasted your code and removed the two lines dealing with your LoginPage , and it works flawlessly for me.

I then created and empty window as a replacement for LoginPage , added the same two lines to show it, and it still works.

I'd guess the problem is in your LoginPage, which must be blocking the UI thread one way or another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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