-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (92 loc) · 2.86 KB
/
Program.cs
File metadata and controls
107 lines (92 loc) · 2.86 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.ReactiveUI;
using MWSManager.Views;
using Projektanker.Icons.Avalonia;
using Projektanker.Icons.Avalonia.MaterialDesign;
using Serilog;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace MWSManager;
sealed class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
bool ret;
new Mutex(true, "MWSManager-MM", out ret);
if (!ret)
{
// Write to the single instance
NamedPipeClientStream client = new NamedPipeClientStream("MWSManager-ModManager");
client.Connect(100);
if (client.IsConnected)
{
using var writer = new BinaryWriter(client);
writer.Write(string.Join(" ", args));
}
// We sent what we are supposed to, exit
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopApp)
{
desktopApp.Shutdown();
}
return;
}
_ = CheckForMessage();
try
{
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
catch (Exception e)
{
Log.Fatal(e, "Something very bad happened");
}
finally
{
Log.CloseAndFlush();
}
}
async static Task CheckForMessage()
{
using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(100));
while (await timer.WaitForNextTickAsync())
{
using var server = new NamedPipeServerStream("MWSManager-ModManager");
server.WaitForConnection();
using var reader = new BinaryReader(server);
try
{
string arguments = reader.ReadString();
if (Application.Current != null)
{
App app = (App)Application.Current;
app.OnMessage(arguments);
}
}
catch {}
server.Disconnect();
}
}
private void HandleIncomingArguments(string args)
{
Trace.WriteLine(args);
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
{
IconProvider.Current.Register<MaterialDesignIconProvider>();
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace()
.UseReactiveUI();
}
}