I am trying to show progressbar when user is trying to login into system. Durin the operation I am showing the progressbar window to the user. I did this using a backgroundworker and it works. But sometime the system crashes. It seems windows 8 is creating the problem. Because it's running in Windows 7 without any error.Here is my loadingview.xaml.cs code which contains the progressbar related code
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();
}
}
Now I am showing you my Loginviewform.xaml.cs code which implements the progressbar.
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;
}
Any idea what is going wrong? Any help would be appreciated.Thanks
It's very strange code. Try refactor it like this:
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();
}
}
And some method :
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;
}
}
That's method looks ugly and you must refactor it to. But i just want to show main idea
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.