0

How to change label in UserControl in UserControl from another thread?

Here is my code:

this.Dispatcher.Invoke(DispatcherPriority.Input,
                  new DelegateMethod(() => {
                    App.mainWindow.FirstUserControl.SecondUserControl.txtLbl.Content = "Hello!";
                  }));

Error:

System.InvalidOperationException: "The thread calling this cannot access the specified object because the object is owned by another thread."
  • This looks about right on first glance. What's your problem? – PMF Mar 07 '22 at 15:53
  • System.InvalidOperationException: "The thread calling this cannot access the specified object because the object is owned by another thread." – MrVIPkaMan Mar 07 '22 at 16:06
  • Are you sure you are running that code and not an old version? Does it compile? Do you run the correct build? – Thomas Weller Mar 07 '22 at 16:10
  • Yes, i am sure. – MrVIPkaMan Mar 07 '22 at 16:14
  • I guess you need to build a [mre] then... – Thomas Weller Mar 07 '22 at 16:15
  • It looks almost as if the second user control was created on a different thread than the first control. Ifs that the case? If so, get the text out of the second user control into a `string` object on your current thread and then send that as a string to the `Dispatcher.Invoke` call. – Joe Mar 07 '22 at 16:29

0 Answers0