Hello,
i have a LogInWindow.xaml.cs in witch i create a thread on a local method, in witch i am trying to call a method from an external class Client.connect(string1,string2); and i get the following exception:
{System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
at System.Windows.Controls.TextBox.get_Text()
at Poker.LogInWindow.callLogIn() in C:\Users\Gaby\Documents\Visual Studio 2010\Projects\Poker\Poker\LogInWindow.xaml.cs:line 86}
what should i do? thanks!
Regards,
Sas Gabriel
Code:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (userInput.Text == "")
MessageBox.Show("Please enter your username.");
else
if (passInput.Password == "")
{
MessageBox.Show("Please enter your password.");
}
else
{
button1.IsEnabled = false;
Thread t = new Thread(new ThreadStart(callLogIn));
t.Start();
}
}
public void callLogIn()
{
try
{
Client.connect(userInput.Text, passInput.Password);
}
catch (Exception e)
{
LogInWindow.btn.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
LogInWindow.btn.IsEnabled = true;
}
));
MessageBox.Show("Could not connect to server. Please try again later. ");
}
}