WinUI: Cockpit laeuft nur einmal - ein zweiter Start holt das laufende nach vorn
Eigenes Main (Programm.cs) mit AppInstance.FindOrRegisterForKey: der zweite Prozess reicht seine Aktivierung weiter und endet; das erste kommt nach vorn und klappt die Pille aus. Geprueft: zwei Starts -> ein Prozess. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,11 +5,20 @@ namespace Pokerfreak.Cockpit;
|
||||
/// <summary>Start: ein Fenster, eine Verbindung zum Server (Kern/Verbindung.cs).</summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
private static App? _laufend;
|
||||
private MainWindow? _fenster;
|
||||
|
||||
/// <summary>Ein zweiter Start wurde hierher gereicht (Programm.cs): das Cockpit nach vorn holen.</summary>
|
||||
public static void Aktiviert()
|
||||
{
|
||||
var f = _laufend?._fenster;
|
||||
f?.DispatcherQueue.TryEnqueue(f.Hervorholen);
|
||||
}
|
||||
|
||||
public App()
|
||||
{
|
||||
InitializeComponent();
|
||||
_laufend = this;
|
||||
// Ein Absturz hinterlaesst seinen Grund in %APPDATA%\Pokerfreakbsturz.txt (sonst sieht man nur
|
||||
// "Microsoft.UI.Xaml.dll" im Ereignisprotokoll).
|
||||
UnhandledException += (_, e) => Seiten.Merker.Absturz(e.Exception);
|
||||
|
||||
@@ -222,7 +222,7 @@ public sealed partial class MainWindow : Window
|
||||
if (_leiste.Pille) _pille.Zeichnen(); else _sichtbar?.Zeichnen();
|
||||
}
|
||||
/// <summary>Aus dem Tray zurueck: zeigen, wiederherstellen, nach vorn.</summary>
|
||||
private void Hervorholen()
|
||||
internal void Hervorholen()
|
||||
{
|
||||
AppWindow.Show();
|
||||
if (AppWindow.Presenter is OverlappedPresenter op && op.State == OverlappedPresenterState.Minimized) op.Restore();
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
<EnableMsixTooling>true</EnableMsixTooling>
|
||||
<ApplicationIcon>Assets/cockpit.ico</ApplicationIcon>
|
||||
<!-- Eigenes Main in Programm.cs: nur ein Cockpit zugleich -->
|
||||
<DefineConstants>$(DefineConstants);DISABLE_XAML_GENERATED_MAIN</DefineConstants>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.260921001" />
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.Windows.AppLifecycle;
|
||||
|
||||
namespace Pokerfreak.Cockpit;
|
||||
|
||||
/// <summary>
|
||||
/// Einstieg statt des generierten Main: das Cockpit laeuft immer nur EINMAL (Owner 16.11.: "war grad
|
||||
/// 2 mal geoeffnet"). Ein zweiter Start (Verknuepfung, Taskleiste) reicht seine Aktivierung an das
|
||||
/// laufende Cockpit weiter - das kommt nach vorn, klappt die Pille aus - und endet sofort.
|
||||
/// </summary>
|
||||
public static class Programm
|
||||
{
|
||||
/// <summary>Der Schluessel, unter dem sich das erste Cockpit anmeldet (je Windows-Sitzung).</summary>
|
||||
public const string Schluessel = "Pokerfreak.Cockpit";
|
||||
|
||||
[STAThread]
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
WinRT.ComWrappersSupport.InitializeComWrappers();
|
||||
if (Weiterreichen())
|
||||
return 0;
|
||||
Microsoft.UI.Xaml.Application.Start(p =>
|
||||
{
|
||||
SynchronizationContext.SetSynchronizationContext(
|
||||
new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread()));
|
||||
new App();
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True: es laeuft schon ein Cockpit, die Aktivierung ist dorthin gereicht - dieser Prozess endet.
|
||||
/// False: dieses ist das erste; spaetere Starts melden sich ueber `Activated`.
|
||||
/// </summary>
|
||||
private static bool Weiterreichen()
|
||||
{
|
||||
AppInstance erstes;
|
||||
try
|
||||
{
|
||||
erstes = AppInstance.FindOrRegisterForKey(Schluessel);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Seiten.Merker.Absturz(e); // lieber zwei Cockpits als keins
|
||||
return false;
|
||||
}
|
||||
if (erstes.IsCurrent)
|
||||
{
|
||||
erstes.Activated += (_, _) => App.Aktiviert();
|
||||
return false;
|
||||
}
|
||||
// Weiterreichen auf einem Hintergrundfaden; der STA-Faden wartet mit COM-Pumpe darauf
|
||||
// (CoWaitForMultipleObjects wie im Muster der Windows App SDK) - hoechstens 5 s.
|
||||
var aktivierung = AppInstance.GetCurrent().GetActivatedEventArgs();
|
||||
var fertig = CreateEvent(IntPtr.Zero, true, false, null);
|
||||
Task.Run(() =>
|
||||
{
|
||||
try { erstes.RedirectActivationToAsync(aktivierung).AsTask().Wait(); }
|
||||
catch { /* das erste endet gerade: dann bleibt es bei keinem neuen Fenster */ }
|
||||
SetEvent(fertig);
|
||||
});
|
||||
_ = CoWaitForMultipleObjects(0, 5000, 1, new[] { fertig }, out _);
|
||||
return true;
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern IntPtr CreateEvent(IntPtr attribute, bool manuell, bool anfangs, string? name);
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern bool SetEvent(IntPtr griff);
|
||||
|
||||
[DllImport("ole32.dll")]
|
||||
private static extern uint CoWaitForMultipleObjects(uint flags, uint millisekunden, ulong anzahl,
|
||||
IntPtr[] griffe, out uint index);
|
||||
}
|
||||
Reference in New Issue
Block a user