• Register
0 votes
98 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?

6 5 3
7,540 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.

 OR

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.

9 7 4
38,600 points

Related questions

1 vote
1 answer 34 views
34 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
2 answers 649 views
649 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 104 views
104 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, 2020 alecxe 7.5k points
0 votes
1 answer 23 views
23 views
Problem I never have developed multithreading applications before, so I am a bit confused how to make one thread to wait for another. In general, I have UI thread and 2 other threads, which do some processing. I need the processing to be executed in different thread (not the UI one) in ... Task t2 = Task.Factory.StartNew(new Action(() => { //Do another stuff here })); } How can I sync t1 and t2?
asked Nov 19, 2020 alexh 2.2k points
1 vote
1 answer 112 views
112 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, 2020 morrisBson 3.2k points
0 votes
1 answer 81 views
81 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 52 views
0 votes
1 answer 20 views
20 views
Problem: I have recently joined a new project in WPF C# and at this time I am trying to animate the image to simply move down. I have already found out the "MoveTo" function on an Internet but when I pasted it in my code then the below error occurred: “ ... button_Click(object sender, RoutedEventArgs e)     {         MoveTo(image, 130);     } } Kindly help me in fixing my above C# code.
asked Jul 28, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 52 views
52 views
Problem : I have tried to built a simple music player in Android. My view for each song contains the SeekBar. It works fine for me. Now I want the timer counting the seconds/minutes of a progress of a song. So I put the TextView in a layout, get it with the ... doing with my SeekBar - creating a view in onCreate, then just touching it in run() – and then it doesn't give me any complaint.
asked Jan 28, 2020 jwilliam 3.9k points
0 votes
2 answers 616 views
616 views
Problem : I am beginner to Android Programming I am trying to draw the plot/chart for the very first time. Currently I am running different kinds of sample codes and my emulator is using three different types of free libraries but nothing is apearing in the layout screen. ... (1378): Skipped 55 frames! The application may be doing too much work on its main thread. How can I fix this issue?
asked Feb 28, 2020 mphil 2.3k points