using System; using System.Threading; using System.Threading.Tasks; using Avalonia.Themes.Fluent; using Avalonia.Threading; namespace Avalonia.IntegrationTests.Win32.Infrastructure; internal static class AppManager { private static readonly Lazy> s_initTask = new(CreateUIThread, LazyThreadSafetyMode.ExecutionAndPublication); private static readonly CancellationTokenSource s_cancellation = new(); public static void Stop() => s_cancellation.Cancel(); private static Task CreateUIThread() { var tcs = new TaskCompletionSource(); var uiThread = new Thread(() => { var appBuilder = AppBuilder .Configure() .UseWin32() .UseSkia() .UseHarfBuzz() .SetupWithoutStarting(); appBuilder.Instance!.Styles.Add(new FluentTheme()); // Ensure that Dispatcher.UIThread is initialized on this thread var dispatcher = Dispatcher.UIThread; dispatcher.VerifyAccess(); tcs.TrySetResult(dispatcher); dispatcher.MainLoop(s_cancellation.Token); }) { Name = "UI Thread" }; uiThread.Start(); return tcs.Task; } public static Task EnsureAppInitializedAsync() => s_initTask.Value; }