static void Main() { App app = new App { ShutdownMode = ShutdownMode.OnExplicitShutdown }; app.InitializeComponent(); SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext()); Program4a p = new Program4a(); p.ExitRequested += (sender, e) => { app.Shutdown(); }; Task programStart = p.StartAsync(); app.Run(); }