Call from invalid thread
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@Gillibald ohhh ok so normal code but at that line:
Thank you it works