Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The calling thread cannot access this object because another thread owns it #143

Closed
593172440 opened this issue Jun 16, 2023 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@593172440
Copy link

In .net framework 4.8 WPF
I New Create UserControl Name "Loading" And apply it Downloader . In another xaml Grid.Children.Add(Loading).Unable to close after download is complete.
prompt:
System.InvalidOperationException:“The calling thread cannot access this object because another thread owns it。”
Ask how to end this object in the loading interface

@bezzad bezzad self-assigned this Jun 18, 2023
@bezzad bezzad added the question Further information is requested label Jun 18, 2023
@bezzad
Copy link
Owner

bezzad commented Jun 18, 2023

When using WPF or another UI framework, it is not possible to directly access the main thread (UI thread) from another thread. Once your downloads are complete and you resume the await task with the result, the current thread may not be the main thread due to the Downloader running scenarios. Therefore, to call UI elements like Grid, you must use the Dispatcher, as shown in the code below:

Grid.Dispatcher.Invoke(new Action(() => Grid.Children.Add(Loading)));

This is a .Net thread management control. For more information, please refer to the following link:

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/threading-model?redirectedfrom=MSDN&view=netframeworkdesktop-4.8

@bezzad bezzad closed this as completed Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants