-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
58 lines (50 loc) · 2.15 KB
/
Program.cs
File metadata and controls
58 lines (50 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Microsoft.Windows.AppLifecycle;
using System;
namespace OpenPatro;
/// <summary>
/// Custom entry point that enforces single-instance behavior.
/// Replaces the auto-generated Main (disabled via DISABLE_XAML_GENERATED_MAIN).
/// </summary>
public static class Program
{
[global::System.STAThread]
static void Main(string[] args)
{
global::WinRT.ComWrappersSupport.InitializeComWrappers();
// Register this process under a well-known key. If a previous instance is
// already registered, IsCurrent == false and we redirect activation to it.
var instance = AppInstance.FindOrRegisterForKey("OpenPatro-SingleInstance");
if (!instance.IsCurrent)
{
// A previous instance is running — redirect activation so it can show
// its main window, then exit this new process immediately.
try
{
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
instance.RedirectActivationToAsync(activationArgs).GetAwaiter().GetResult();
}
catch
{
// Redirection failure is non-fatal; the user will still see only one
// tray icon because this process exits right after.
}
return;
}
// This is the primary (or only) instance.
// When a subsequent launch tries to redirect here, show the main window.
instance.Activated += OnInstanceActivated;
global::Microsoft.UI.Xaml.Application.Start(p =>
{
var context = new global::Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext(
global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread());
global::System.Threading.SynchronizationContext.SetSynchronizationContext(context);
_ = new App();
});
}
private static void OnInstanceActivated(object? sender, AppActivationArguments args)
{
// Another launch was redirected to us — bring the main window into view.
var app = global::Microsoft.UI.Xaml.Application.Current as App;
app?.ShowMainWindow();
}
}