-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.axaml.cs
More file actions
135 lines (117 loc) · 4.25 KB
/
Copy pathApp.axaml.cs
File metadata and controls
135 lines (117 loc) · 4.25 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace RemoteHub
{
public partial class App : Application
{
private static Mutex? mutex;
private static bool mutexAcquired = false;
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
bool isNewInstance = AcquireCrossPlatformMutex("RemoteHub_Mutex");
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
desktop.Exit += OnAppExit;
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}
base.OnFrameworkInitializationCompleted();
}
#region Manipulation faite suite a la fermeture - crash du logiciel
// Méthode appelée lorsque l'application se termine proprement
private void OnAppExit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
{
DeleteFile();
ReleaseMutex();
}
// Méthode appelée lorsque le processus est tué ou termine
private void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
DeleteFile();
ReleaseMutex();
}
private void ReleaseMutex()
{
if (mutexAcquired && mutex is not null)
{
try { mutex.ReleaseMutex(); } catch { }
}
}
private void DeleteFile()
{
string appDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "RemoteHubData");
string filePath = System.IO.Path.Combine(appDirectory, "RDA_decrypted.dat");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
string filePath2 = System.IO.Path.Combine(appDirectory, "RDA.dat");
if (File.Exists(filePath2))
{
long fileSize = new FileInfo(filePath2).Length;
if (fileSize == 0 && File.Exists(filePath2))
{
File.Delete(filePath2);
}
}
}
//Mutex cross-plateforme
private bool AcquireCrossPlatformMutex(string mutexName)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
mutex = new Mutex(true, mutexName, out bool isNewInstance);
mutexAcquired = isNewInstance;
return isNewInstance;
}
else
{
string lockPath = Path.Combine(Path.GetTempPath(), mutexName + ".lock");
try
{
if (File.Exists(lockPath))
{
// Vérifier si le processus existe toujours
try
{
string pidStr = File.ReadAllText(lockPath);
if (int.TryParse(pidStr, out int pid))
{
Process.GetProcessById(pid);
// Le processus existe, on ne peut pas acquérir le mutex
return false;
}
}
catch
{
// Le processus n'existe plus, supprimer le fichier
File.Delete(lockPath);
}
}
File.WriteAllText(lockPath, Process.GetCurrentProcess().Id.ToString());
mutexAcquired = true;
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
{
try { File.Delete(lockPath); } catch { }
};
return true;
}
catch
{
return false;
}
}
}
#endregion
}
}