Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);//this is where i am getting the exception
            }
            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. ");
            }
        }
Posted
Updated 25-Nov-11 22:49pm
v6
Comments
Sergey Alexandrovich Kryukov 25-Nov-11 17:12pm    
It needs your code sample to see what's going on. A code sample, minimal but comprehensive sample to manifest a problem, not all of your application. Please use "Improve question".
--SA

1 solution

You can't access that TextBox (or any other UI element) from another thread. You'll need to invoke the code accessing UI elements on the UI thread.

WPF Threading Model[^]
 
Share this answer
 
Comments
Gabriel Sas 26-Nov-11 4:20am    
this i know allready because in my application i am allready calling UI elements from another thread, but now what i want to call from another thread is a method from a class. and i dont know if the dispatcher can call methods, didnt find anything on google
Mark Salsbery 26-Nov-11 11:00am    
If you already know this then why aren't you doing it? The exception is coming from a textbox so you know you have to use the dispatcher. You're always invoking a mehod, not "calling UI elements".
Gabriel Sas 26-Nov-11 13:46pm    
pff i just realize what did not work, the userInput can't be access , and i just need to put a dispatcher on textbox and passwordbox, i thought the problem was calling the method from another thread and i didn't see the problem and didn't catch exactly why are you talking about the textbox :).

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900