nizesh
Topic Author
Posts: 10
Joined: 31 Jul 2019, 22:32

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

07 Aug 2019, 18:03

I'm trying to render a CustomCanvas view programmatically
 public class GraphCanvas : Canvas {
 	...
 	public void renderGraph()
        {
            Rectangle rect = new Rectangle();
            rect.Width = 200;
            rect.Height = 100;
            rect.Fill = Brushes.Blue;
            this.Children.Add(rect);
          }
 }
I'm calling this method
renderGraph();
from another class but it's giving me this error
The calling thread cannot access this object because a different thread owns it.
What's the fix for this?

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2561
Joined: 20 Jan 2012, 17:18
Contact:

Re: The calling thread cannot access this object because a different thread owns it.

08 Aug 2019, 12:30

Are you invoking that function from a different thread than the one used to create the object?
 
nizesh
Topic Author
Posts: 10
Joined: 31 Jul 2019, 22:32

Re: The calling thread cannot access this object because a different thread owns it.

08 Aug 2019, 16:47

Yeah. The place from where I'm calling that function is the place where I receive my data. The logic I'm trying to implement is whenever the data gets updated, the UI needs to be invalidated as well. I think that error message is showing up because I'm trying to access the UI thread from the main thread but I don't know how to get over it. Apparantly, the
Dispatcher.Invoke
method didn't seem to work. What ways can we invalidate the UI through a thread outside the UI thread?
 
User avatar
jsantos
Site Admin
Posts: 2561
Joined: 20 Jan 2012, 17:18
Contact:

Re: The calling thread cannot access this object because a different thread owns it.

08 Aug 2019, 17:14

Noesis objects created in one thread cannot be accessed from a different thread. We don't provide a mechanism to solve that, you need to do it yourself, depending on the platform you are using.

If Dispatcher is not available in Unity, what the recommended mechanism then?

Who is online

Users browsing this forum: Google [Bot], sfernandez and 3 guests