dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.17k stars 1.74k forks source link

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

Open jonmdev opened 1 year ago

jonmdev commented 1 year ago

Description

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

https://github.com/dotnet/maui/issues/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.

ghost commented 1 year ago

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 commented 1 year ago

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:

https://github.com/dotnet/maui/issues/18070#event-10685211681

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

ghost commented 11 months ago

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 commented 4 days ago

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 commented 3 days ago

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.