• Register
1 vote

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?

6 5 3
7,540 points

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



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.

9 7 4
38,600 points

Related questions

0 votes
1 answer 79 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 alecxe 7.5k points
0 votes
2 answers 420 views
Problem : I want to save on the DB the log of a multi threaded processor but I am facing the following error: new transaction is not allowed because there are other threads running in the session. In each tread I have following function : internal bool WriteMyTrace(IResult ... for each of the threads, but there connection with my DB is always in the same way. Anyone have solution on my problem?
asked Dec 5, 2019 alecxe 7.5k points
0 votes
1 answer 71 views
Problem : I have some of the code and when I try to executes it, it throws the below IOException, saying that “The process cannot access the file 'filename' because it is being used by another process” What is the meaning of this exception , and how can I completely fix the exception?
asked Jan 3 alecxe 7.5k points
1 vote
1 answer 45 views
Problem : Currently I am learning the use of HttpClient class. Also I have started using the HttpClient class to communicate with the web service in my current WPF application. If I try to make the consecutive GET requests on my same connection then everything works perfectly. But ... working for me. I am unable to fix above error. I am looking for somebody who can help me in fixing above error.
asked Apr 2 morrisBson 3.2k points
0 votes
1 answer 26 views
Problem: I am naïve to C# programming. I am trying to use the auto-implemented properties. I think the best ever way to fix below error is to declare my own backing variable? Please find below my C# code: public Point Origin { get; set; } ... unchanged. To resolve this error, store the result of the expression in an intermediate value, or use a reference type for the intermediate expression.
asked Jul 23 Raphael Pacheco 4.9k points