当用户尝试登录系统时,我试图显示进度条。在操作期间,我向用户显示进度条窗口。我使用后台工作人员做到了这一点,它可以工作。但有时系统崩溃。似乎 Windows 8 正在制造这个问题。因为它在 Windows 7 中运行而没有任何错误。这是我的 loadingview.xaml.cs 代码,其中包含与进度条相关的代码
public partial class LoadingViewControl : Window
{
System.ComponentModel.BackgroundWorker mWorker;
public LoadingViewControl()
{
InitializeComponent();
Load();
}
public LoadingViewControl(bool Close)
{
this.Close();
}
public void Load()
{
mWorker = new System.ComponentModel.BackgroundWorker();
mWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
mWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
mWorker.WorkerReportsProgress = true;
mWorker.WorkerSupportsCancellation = true;
mWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
mWorker.RunWorkerAsync();
while (pbProcessing.Value < 99)
{
if (!mWorker.CancellationPending)
{
try
{
if (pbProcessing.Value > 95)
{
mWorker.CancelAsync();
//Uri uri = new Uri("/View/LoginchildView.xaml", UriKind.Relative);
break;
}
else
{
pbProcessing.Value = (pbProcessing.Value + 0.005) % 100;
}
}
catch (System.Exception ex)
{
// No action required
}
}
else
{
break;
}
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
new System.Threading.ThreadStart(delegate { }));
}
}
private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// Do your work here, its on seperate thread
}
private void worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
pbProcessing.Value = e.ProgressPercentage;
}
private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
// Stop Progressbar updatation
}
private void LoadingViewControl_Load(object sender, EventArgs e)
{
SetFocus();
}
public void SetFocus()
{
this.Focus();
this.Activate();
}
}
现在,我将向您展示实现进度条的 Loginviewform.xaml.cs 代码。
if (txtUserName.Text != "")
{
if (txtPassword.Password != "")
{
if (STAThread == null)
{
STAThread = new Thread(() => { new LoadingViewControl().ShowDialog(); });
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.IsBackground = true;
STAThread.Start();
}
else
{
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.IsBackground = true;
STAThread.Start();
}
result = User.Instance.Authenticicate(txtUserName.Text, txtPassword.Password);
if (result == true)
{
pbProcessing.Value = 100;
ServiceLocator.Current.GetInstance<ContainerViewModel>().ExecuteLobbyBasicViewCommand();
//redirect on specific page.
if (STAThread.IsAlive)
{
Thread.CurrentThread.Interrupt();
STAThread.Interrupt();
STAThread.Abort();
STAThread = null;
}
PopUp objpopup = new PopUp();
objpopup.txtNotice.Text = "sign in";
objpopup.txtMessage.Text = "successfully sign in.";
objpopup.ShowDialog();
Global.GetUserName = txtUserName.Text;
}
else
{
//MessageBox.Show("Sign In was unsuccessful. Please correct the errors and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
STAThread.Abort();
STAThread = null;
PopUp objPopUp = new PopUp();
objPopUp.txtNotice.Text = "Error";
objPopUp.txtMessage.Text = "Sign In was unsuccessful. Please correct the errors and try again.";
objPopUp.ShowDialog();
txtUserName.Focus();
//pbProcessing.Value = 0;
stackpanelLoading.Visibility = System.Windows.Visibility.Hidden;
}
}
else
{
//MessageBox.Show("Please enter the valid Password.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
PopUp objPopUp = new PopUp();
objPopUp.txtNotice.Text = "Error";
objPopUp.txtMessage.Text = "Please enter the valid Password.";
objPopUp.ShowDialog();
txtPassword.Focus();
stackpanelLoading.Visibility = System.Windows.Visibility.Hidden;
}
}
else
{
//MessageBox.Show("Please enter the valid Player ID.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
PopUp objPopUp = new PopUp();
objPopUp.txtNotice.Text = "Error";
objPopUp.txtMessage.Text = "Please enter the valid Player ID.";
objPopUp.ShowDialog();
txtUserName.Focus();
stackpanelLoading.Visibility = System.Windows.Visibility.Hidden;
}
知道出了什么问题吗?任何帮助将不胜感激。谢谢