2

ユーザーがシステムにログインしようとしているときにプログレスバーを表示しようとしています。操作中にプログレスバーウィンドウをユーザーに表示しています。私はbackgroundworkerを使用してこれを行い、それは機能します。しかし、システムがクラッシュすることがあります。Windows8が問題を引き起こしているようです。エラーなしでWindows7で実行されているため、プログレスバー関連のコードを含む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;
        }

何がうまくいかないのか分かりますか?助けていただければ幸いです。ありがとう

4

1 に答える 1

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;
            }
        }

そのメソッドは見た目が悪いので、リファクタリングする必要があります。しかし、私はただ主なアイデアを示したいだけです

于 2013-03-21T14:01:26.113 に答える