I’m not sure if I’m doing this correctly, but I have the following code that I am using (click button1, do the _DoWork). The question is this: how do I call the UI to get the values of textbox1 and textbox2 as they cannot be called as they are on a different thread. Should I be using dispatchers?
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);
}
}
}
Should I be using background worker?
You can pass data into the worker via the argument of the RunWorkerAsync call and pass data out via DoWorkEventArgs.Result…