• Register
0 votes

Problem :

I am facing error on below step 

objUDMCountryStandards.Country = txtMySearchCountry.Text.Trim() != string.Empty ? txtMySearchCountry.Text : null; 

in the get grid data throws error as below :

“The calling thread cannot access this object because a different thread owns it.”

Is there anything wrong with my code?

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I had also faced the similar issue in the recent past. I did lot of research on it and found the solution on it.This is the very common problem with the people getting started. Whenever you need to update your UI elements from the thread other than a main thread, you need to use as below :

this.Dispatcher.Invoke(() =>
    ...// your code goes here.

You can also try to use control.Dispatcher.CheckAccess() to check whether your current thread owns a control. If it does own it then your code looks as the normal. Otherwise, use the above mentioned pattern.


Another good use for the Dispatcher.Invoke is for the immediately updating your UI in the function that performs the other tasks:

// Force WPF to render the UI changes immediately with below magic line of code...
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);

I use above code to update button text to "Processing..." and then disable it while making the WebClient requests.

38.6k points

Related questions

1 vote
1 answer 59 views
Problem : In the following step objUDMCountryStandards.Country = txtSearchCountry.Text.Trim() != string.Empty ? txtSearchCountry.Text : null; in get grid data throws the exception as below : The calling thread cannot access this object because a different thread owns it. What is wrong in my code?
asked Dec 28, 2019 alecxe 7.5k points
0 votes
1 answer 17 views
Problem: I'm seeking for a solution and would appreciate some help and support: The calling thread cannot access this object because a different thread owns it.
asked Jun 14 asha 121k points
0 votes
1 answer 9 views
Problem : How does the calling thread access this object because a different thread owns it? Please assist me.
asked May 27 Sifat55 108k points
0 votes
1 answer 3 views
Problem: Is there any way for me to solve this : The calling thread must be sta, because many ui components require this.
asked Jun 19 Diederich Raf 120k points
0 votes
1 answer 43 views
Problem: The application called an interface that was marshalled for a different thread.
asked Apr 28 Ryan evans 1.6k points
0 votes
1 answer 7 views
Problem: What should I do if I can't find a solution anywhere : The method used to arrange for a button to notify another object when it is clicked later is to an?
asked Jun 10 Nahil 128k points
0 votes
1 answer 24 views
Problem: Asked few friends however no response, please help : The thread tried to read from or write to a virtual address for which it does not have access
asked Jun 11 nila 121k points
0 votes
1 answer 10 views
Problem: Different ways to create thread in java.
asked Jul 20 Shima 126k points
0 votes
1 answer 68 views
We have a company email form. So, we have various clients and e-mail lists. What we required to execute a feedback form which E-mails us the details on competition.
asked Sep 15, 2020 Daniel Anderson 4k points
0 votes
1 answer 12 views
Problem: Runtime error r6016 not enough space for thread data?
asked Jun 26 Nahil 128k points