• Register
1 vote
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?

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

The problem is that you are trying to call the GetGridData from the background thread. This method generally accesses several WPF controls which are connected to your main thread. Any attempt to access them from the background thread will lead to your error.

So in order to get back to your correct thread you must use below approach :  SynchronizationContext.Current.Post.

But in your particular case it appears like your majority of the work that you are doing is UI based. So you must be creating the background thread just to go immediately back to your UI thread and do some relevant work. You need to completely refractor your code so that it can do your expensive work on your background thread and then it can post the new data to your UI thread afterwards

OR

 

Also, another solution for you is ensuring that your controls are created in UI thread only and not by the background worker thread for example.

38.6k points

Related questions

0 votes
1 answer 233 views
233 views
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?
asked Jan 7, 2020 alecxe 7.5k points
0 votes
1 answer 17 views
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
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
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
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
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
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
10 views
Problem: Different ways to create thread in java.
asked Jul 20 Shima 126k points
0 votes
1 answer 68 views
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
12 views
Problem: Runtime error r6016 not enough space for thread data?
asked Jun 26 Nahil 128k points