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

MainThread.InvokeOnMainThreadAsync() when called from non-UI thread says "Unable to find main thread" in Windows #18221

Open
jonmdev opened this issue Oct 21, 2023 · 5 comments
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/windows 🪟 t/bug Something isn't working
Milestone

Comments

@jonmdev
Copy link

jonmdev commented Oct 21, 2023

Description

I note the thread here from 2 years ago which is locked so I couldn't reply:

#2451

This is still an issue. If you attempt to use MainThread.InvokeOnMainThreadAsync() from a non-UI thread in Windows in .NET 7 it replies with an exception Unable to find main thread.

I see in that in that thread @mattleibow says "we probably need to remove MainThread from essentials because it make no sense because windows has no root window and there may be multiple threads, each with a window. So which to pick?"

So is it a given this function doesn't work in WIndows and we shouldn't use it? Or is it a bug?

I couldn't tell from the conclusion of that thread: Should this function work or not?

I note using Application.Current.Dispatcher.DispatchAsync() otherwise identically does seem to work in Windows. And I tested it in Android/iOS and it seems to work there too. So I presume we should use that instead?

If MainThread.InvokeOnMainThreadAsync() is not expected to be Windows safe then perhaps it should be removed?

Thanks.

Version with bug

7.0.96

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Did you find any workaround?

Application.Current.Dispatcher.DispatchAsync() works fine for the same function in at least Windows/Android/iOS.

@jonmdev jonmdev added the t/bug Something isn't working label Oct 21, 2023
@jonmdev jonmdev changed the title MainThread.InvokeOnMainThreadAsync() when called from non-UI thread "Unable to find main thread" in Windows MainThread.InvokeOnMainThreadAsync() when called from non-UI thread says "Unable to find main thread" in Windows Oct 21, 2023
@jsuarezruiz jsuarezruiz added the area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info label Oct 23, 2023
@PureWeen PureWeen added the s/try-latest-version Please try to reproduce the potential issue on the latest public version label Oct 24, 2023
@ghost
Copy link

ghost commented Oct 24, 2023

Hi @jonmdev. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@jonmdev
Copy link
Author

jonmdev commented Oct 24, 2023

Thanks but I will not bother trying new version or making a repro for this one.

I can't use .net 8 due to this bug:

#18070 (comment)

Application.Current.Dispatcher.DispatchAsync() works fine and I will keep using it.

@ghost ghost removed the s/try-latest-version Please try to reproduce the potential issue on the latest public version label Oct 24, 2023
@PureWeen PureWeen added this to the Backlog milestone Nov 3, 2023
@ghost
Copy link

ghost commented Nov 3, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@MitchBomcanhao
Copy link

now running into this issue, where the debug version of the app works fine, but when building said app it constantly crashes due to these errors.
this is the same code that ran fine in xamarin :/

@jonmdev
Copy link
Author

jonmdev commented Oct 23, 2024

now running into this issue, where the debug version of the app works fine, but when building said app it constantly crashes due to these errors. this is the same code that ran fine in xamarin :/

This is the workaround I made. Just create this as a static class and use this instead:

    public static class MainThreadRunner {

        public static bool runSynchronous(Action action) {
#if WINDOWS
            return Application.Current.Dispatcher.Dispatch(action); //note this only schedules on main thread, no guarantee of when will run
#else
            MainThread.BeginInvokeOnMainThread(action); //runs immediately if already on main thread
            return true;
#endif
        }
        public static Task runAsync(Action action) {
#if WINDOWS
            return Application.Current.Dispatcher.DispatchAsync(action);
#else
            return MainThread.InvokeOnMainThreadAsync(action);
#endif
        }
    }

I am not sure why or when this bug happens. It is not consistent or easy to trigger. I just tried and couldn't come up with a bug project to reproduce it. Either way the above should fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/windows 🪟 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants