File: MainThread\MainThread.uwp.cs
Web Access
Project: src\src\Essentials\src\Essentials.csproj (Microsoft.Maui.Essentials)
#nullable enable
using System;
using Microsoft.UI.Dispatching;
 
namespace Microsoft.Maui.ApplicationModel
{
	public static partial class MainThread
	{
		static bool PlatformIsMainThread =>
			TryGetDispatcherQueue()?.HasThreadAccess ?? false;
 
		static void PlatformBeginInvokeOnMainThread(Action action)
		{
			var dispatcher = TryGetDispatcherQueue();
 
			if (dispatcher == null)
				throw new InvalidOperationException("Unable to find main thread.");
 
			if (!dispatcher.TryEnqueue(DispatcherQueuePriority.Normal, () => action()))
				throw new InvalidOperationException("Unable to queue on the main thread.");
		}
 
		static DispatcherQueue? TryGetDispatcherQueue() =>
			DispatcherQueue.GetForCurrentThread() ??
			WindowStateManager.Default.GetActiveWindow(false)?.DispatcherQueue;
	}
}