我正在尝试在用户尝试登录系统时显示进度条。在操作中我正在向用户显示进度条窗口。我使用背景工作者这样做,它的工作原理。但有时系统崩溃了。似乎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;
}
知道出了什么问题吗?任何帮助将不胜感激。谢谢
答案 0 :(得分:0)
这是非常奇怪的代码。尝试重构它:
public partial class LoadingViewControl : Window
{
public LoadingViewControl(string userName, string password)
{
InitializeComponent();
Initialize(userName, password);
}
private void Initialize(string userName, string password)
{
pbProcessing.IsIndeterminate = true;
var thread = new Thread(() =>
{
DialogResult = User.Instance.Authenticicate(userName, password);
Close();
});
thread.IsBackground = true;
thread.Start();
}
}
还有一些方法:
public void Do()
{
if (string.IsNullOrEmpty(txtUserName.Text))
{
if (string.IsNullOrEmpty(txtPassword.Password))
{
var view = new LoadingViewControl(txtUserName.Text, txtPassword.Password);
result = view.ShowDialog();
if (result == true)
{
ServiceLocator.Current.GetInstance<ContainerViewModel>().ExecuteLobbyBasicViewCommand();
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);
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();
stackpanelLoading.Visibility = Visibility.Hidden;
}
}
else
{
PopUp objPopUp = new PopUp();
objPopUp.txtNotice.Text = "Error";
objPopUp.txtMessage.Text = "Please enter the valid Password.";
objPopUp.ShowDialog();
txtPassword.Focus();
stackpanelLoading.Visibility = Visibility.Hidden;
}
}
else
{
PopUp objPopUp = new PopUp();
objPopUp.txtNotice.Text = "Error";
objPopUp.txtMessage.Text = "Please enter the valid Player ID.";
objPopUp.ShowDialog();
txtUserName.Focus();
stackpanelLoading.Visibility = Visibility.Hidden;
}
}
这个方法看起来很难看,你必须重构它。但我只想表现出主旨