简体   繁体   中英

How to get update from BackgroundWorker.DoWork Event

In my login window, when I click the login button, the configuration and login processes will be executed and those methods are in another class file. So far what I've achieved is when I clicked the login button, the loading animation will be displayed on top of the login window and those processes will be executed as well. There are some login error checking in the configuration class file, so when login error caught, a message box with relevant info will be prompted out and stop the login process, the problem is the message box will not be prompted out since I put those config and login process in the BackgroundWorker.DoWork event.

Here's the codes for Login.xaml.cs:

 private void LoginBtn_Click(object sender, RoutedEventArgs e) 
 {
   Loading.Visibility = Visibility.Visible; //The loading animation

   Loading.Visibility = Visibility.Visible;
   Cursor = Cursors.Wait;

   bw.DoWork += new DoWorkEventHandler(LoginInfoVerification);
   bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
   bw.WorkerReportsProgress = true;
   bw.RunWorkerAsync();
 }

private void LoginInfoVerification(object sender, DoWorkEventArgs e) {
  var loginInfoVerification = config.ServerConnection(loginInfo.userName,
    loginInfo.galPassword, loginInfo.place,
    loginInfo.host, loginInfo.port,
    loginInfo.application);
}

private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  if (GlobalVariables.loginSuccess == true) 
  {
    //Pass these variables to main window
    var mainWindow = new MainWindow(loginInfo.userName, loginInfo.place, loginInfo.host, loginInfo.port,
                                    loginInfo.application);
    mainWindow.Show(); 

    this.Close();
  } 

  else
    Loading.Visibility = Visibility.Collapsed;

  Cursor = Cursors.Arrow;
}

For Configuration.cs:

public Configuration ConfigServerConnection(string loginUserName, string loginPassword, string loginPlace, string loginHost, int loginPort, string loginApplication)
{
  //Server connection
  //Login info verification
  //If login error caught, prompt message box, different errors, different message
 }

Is there any better suggestion in handling UI update and process update at the same time for my case? Please help.

To display the message box you need to switch back to the UI thread with Dispatcher.Invoke , see this .

Application.Current.Dispatcher.Invoke(() => /* show appropriate message box */);

Alternatively if you are using .NET 4.5 or higher you can make your life a lot easier with async-await by marking LoginBtn_Click with the async keyword and then awaiting the log in process.

If there is an asynchronous version of ServerConnection that returns a task you can just await that otherwise you can use Task.Run() to execute ServerConnection on a thread pool thread.

Await will kick off the log in operation asynchronously and once complete will resume the rest of the method on the GUI thread so you can manipulate GUI components without using Dispatcher.Invoke .

private async void LoginBtn_Click(object sender, RoutedEventArgs e) 
 {
   Loading.Visibility = Visibility.Visible; //The loading animation

   Loading.Visibility = Visibility.Visible;
   Cursor = Cursors.Wait;      

   LoginVerification loginInfoVerification = null;

   await Task.Run(() =>
                 {
                     loginInfoVerification = config.ServerConnection(loginInfo.userName, 
                                                                     loginInfo.galPassword, 
                                                                     loginInfo.place, 
                                                                     loginInfo.host, 
                                                                     loginInfo.port, 
                                                                     loginInfo.application);
                 });   

      .. rest of code, check login success, show message box..
 }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM