下面是我用来在window1上显示进度条的一些代码。但是,当我在主窗口上完成工作时,进度条会冻结,然后跳转到后台工作人员所处的阶段。无论如何,进度条会不断更新。即使在其他窗口中完成工作
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.");
}
答案 0 :(得分:2)
我复制粘贴了您的代码并删除了涉及LoginPage
的两行,并且它对我来说完美无瑕。
然后我创建并清空窗口作为LoginPage
的替代,添加了相同的两行来显示它,它仍然有效。
我猜这个问题出在你的LoginPage中,它必须以这种或那种方式阻止UI线程。