using System.Threading; using System.Windows.Threading; using JetBrains.Annotations; using Torch.Managers; namespace Torch.Server.Managers; public class UiManager(TorchServer torchInstance) : Manager(torchInstance) { [CanBeNull] private Thread _uiThread; [CanBeNull] private Dispatcher _dispatcher; public override void Attach() { _uiThread = new Thread(() => { var ui = new TorchUI(torchInstance); _dispatcher = Dispatcher.CurrentDispatcher; SynchronizationContext.SetSynchronizationContext( new DispatcherSynchronizationContext(_dispatcher)); ui.ShowDialog(); }); _uiThread.SetApartmentState(ApartmentState.STA); _uiThread.Start(); } public override void Detach() { _dispatcher?.InvokeShutdown(); if (Thread.CurrentThread != _uiThread) _uiThread?.Join(); } }