背景工作者访问UI的正确方法

时间:2011-02-22 02:48:29

标签: c# wpf backgroundworker

我不确定我是否正确执行此操作,但我使用了以下代码(单击button1,执行_DoWork)。问题是:如何调用UI来获取textbox1和textbox2的值,因为它们不能被调用,因为它们位于不同的线程上。我应该使用调度员吗?

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("Please enter a username and password", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
        }
        else
        {
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerAsync();
        }
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Console.WriteLine("asd");
        UserManagement um = new UserManagement(sm.GetServerConnectionString());
        if (um.AuthUser(textBox1.Text, textBox2.Password))
        {
            MainWindow mw = new MainWindow();
            mw.Show();
            this.Close();
        }
        else
        {
            if (um.Timeout)
            {
                MessageBox.Show("Could not connect to server, please check your configuration", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                MessageBox.Show("Incorrect username or password", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }

我应该使用后台工作人员吗?

4 个答案:

答案 0 :(得分:13)

您可以通过RunWorkerAsync调用的参数将数据传递给worker,并通过DoWorkEventArgs.Result传递数据......

  class AuthUserData
  {
    public string Name;
    public string Password;
  }

  private void button1_Click(object sender, EventArgs e)
  {
     var authData = new AuthUserData() { Name = textBox1.Text, Password = textBox2.Text };
     worker.RunWorkerAsync(authData);
  }

  void worker_DoWork(object sender, DoWorkEventArgs e)
  {
     // On the worker thread...cannot make UI calls from here.
     var authData = (AuthUserData)e.Argument;
     UserManagement um = new UserManagement(sm.GetServerConnectionString());
     e.Result = um;
     e.Cancel = um.AuthUser(textBox1.Text, textBox2.Password));
  }

  void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
     // Back on the UI thread....UI calls are cool again.
     var result = (UserManagement)e.Result;
     if (e.Cancelled)
     {
        // Do stuff if UserManagement.AuthUser succeeded.
     }
     else
     {
        // Do stuff if UserManagement.AuthUser failed.
     }
  }

答案 1 :(得分:3)

顾名思义,后台工作程序不在UI线程上运行。您只能在UI线程上访问UI控件。解决此问题的一种简单方法是在需要新“对象”时保存所需的文本框属性,然后将其传递给RunWorkerAsync。此对象可用于e.Argument中的DoWork方法。

但是,在工作线程上显示表单也存在问题。

答案 2 :(得分:0)

您无法直接从 BackgroundWorker 访问UI元素。为此,您必须使用 Dispatcher 。从DependencyObject派生的WPF对象具有线程关联性,这意味着只有实例化它们的线程才能访问其成员。

检查下面的链接,看看代码示例是否可以帮助您

http://social.msdn.microsoft.com/Forums/en/wpf/thread/4858bcaf-1cb2-410b-989a-18b874ffa458

答案 3 :(得分:-1)

this.Dispather.Invoke((Action)delegate(){
    this.Close();
});