question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call from invalid thread

See original GitHub issue

Describe the bug In my code I am doing this, in an async void called Connect, which is the function to be called for a button:

this.FindControl<ImageControl>("Screen").Source = new Bitmap("screen.png");

But it gives me an error:

System.InvalidOperationException
  HResult=0x80131509
  Message=Call from invalid thread
  Source=Avalonia.Base
  StackTrace:
   at Avalonia.Threading.Dispatcher.VerifyAccess() in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 48
   at Avalonia.AvaloniaObject.GetValue[T](StyledPropertyBase`1 property) in /_/src/Avalonia.Base/AvaloniaObject.cs:line 252
   at Avalonia.Controls.NameScope.GetNameScope(StyledElement styled) in /_/src/Avalonia.Styling/Controls/NameScope.cs:line 37
   at Avalonia.Controls.NameScopeExtensions.<>c.<FindNameScope>b__4_0(StyledElement x) in /_/src/Avalonia.Styling/Controls/NameScopeExtensions.cs:line 114
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at Avalonia.Controls.NameScopeExtensions.FindNameScope(ILogical control) in /_/src/Avalonia.Styling/Controls/NameScopeExtensions.cs:line 116
   at Avalonia.Controls.ControlExtensions.FindControl[T](IControl control, String name) in /_/src/Avalonia.Controls/ControlExtensions.cs:line 59
   at ScreenShareClient.MainWindow.<Connect>b__2_1(SocketIOResponse data) in C:\Users\HP\Desktop\Socket.IO Screen Share\Client\MainWindow.axaml.cs:line 46
   at SocketIOClient.Packgers.MessageEventPackger.Unpack(SocketIO client, String text)
   at SocketIOClient.Packgers.MessagePackger.Unpack(SocketIO client, String text)
   at SocketIOClient.Packgers.PackgeManager.Unpack(String msg)
   at SocketIOClient.WebSocketClient.ClientWebSocket.<ListenAsync>d__16.MoveNext()

Expected behavior For the image source to change

Desktop (please complete the following information):

  • OS: Windows 10
  • Version: 0.10.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
monke8555commented, Jun 2, 2021

@Gillibald ohhh ok so normal code but at that line:

Dispatcher.UIThread.InvokeAsync(async () => {
     this.FindControl < ImageControl > ("Screen").Source = new Bitmap("screen.png");
})
0reactions
monke8555commented, Jun 2, 2021

Thank you it works

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Call from invalid thread" if add item to ...
It looks like that your OnDeviceAdded callback happens on non-UI thread. Use Dispatcher.UIThread.Invoke to schedule a callback on the UI thread.
Read more >
c# - InvalidOperationException: The calling thread cannot ...
The calling thread cannot access this object because a different thread owns it. Code: public partial class MainWindow : Window { Thread t;...
Read more >
How to make thread-safe calls to controls - Windows Forms ...
The InvalidOperationException always occurs for unsafe cross-thread calls during Visual Studio debugging, and may occur at app runtime. You ...
Read more >
Dispatcher.VerifyAccess Method (System.Windows. ...
Determines whether the calling thread has access to this Dispatcher. ... If the calling thread does not have access, an InvalidOperationException is thrown....
Read more >
FAQ Why do I get an invalid thread access exception?
Just about any SWT method that accesses or changes a widget must be called in the UI thread. If you are unsure, check...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found