简体   繁体   English

WPF C#进度条不会更新,直到在后台工作完成工作后

[英]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. 以下是一些我用来在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. 我复制粘贴了您的代码,并删除了处理您的LoginPage的两行,这对我来说是完美的。

I then created and empty window as a replacement for LoginPage , added the same two lines to show it, and it still works. 然后,我创建了一个空窗口来代替LoginPage ,并添加了相同的两行来显示它,但它仍然有效。

I'd guess the problem is in your LoginPage, which must be blocking the UI thread one way or another. 我猜问题出在您的LoginPage中,它必须以某种方式阻止UI线程。

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

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