Revize 30c849e4
Přidáno uživatelem Jakub Šilhavý před asi 2 roky(ů)
ld_client/LDClient/Program.cs | ||
---|---|---|
5 | 5 |
using LDClient.utils; |
6 | 6 |
using LDClient.utils.loggers; |
7 | 7 |
|
8 |
using static System.Diagnostics.Process; |
|
9 |
using static System.Reflection.Assembly; |
|
10 |
|
|
8 | 11 |
namespace LDClient; |
9 | 12 |
|
10 |
internal class Program { |
|
13 |
internal static class Program {
|
|
11 | 14 |
|
12 |
public static ConfigLoader Config { get; set; } = new();
|
|
15 |
public static ConfigLoader Config { get; } = new(); |
|
13 | 16 |
public static ALogger DefaultLogger { get; } = ALogger.Current; |
14 |
|
|
15 |
public static IApiClient DefaultApiClient { get; set; } = new ApiClient(Config.ApiBaseAddress, |
|
16 |
Config.ApiPort, Config.ApiUsbEndPoint, Config.RetryPeriod, Config.MaxEntries, |
|
17 |
Config.MaxRetries, Config.CacheFileName); |
|
18 |
|
|
19 |
|
|
20 |
private static NetworkDetection _netDetect = new(Config.ApiPort, Config.DetectionPeriod); |
|
21 |
private static ProcessDetection _procDetect = new(Config.T32ProcessName, Config.DetectionPeriod); |
|
22 |
|
|
23 |
|
|
17 |
private static IApiClient? DefaultApiClient { get; set; } |
|
18 |
private static readonly NetworkDetection NetDetect = new(Config.ApiPort, Config.DetectionPeriod); |
|
19 |
private static readonly ProcessDetection ProcDetect = new(Config.T32ProcessName, Config.DetectionPeriod); |
|
20 |
|
|
24 | 21 |
// Main Method |
25 |
public static async Task Main() { |
|
22 |
public static int Main() { |
|
23 |
var exists = GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1; |
|
24 |
if (exists) { |
|
25 |
DefaultLogger.Error("Another instance of the application is already running"); |
|
26 |
return 1; |
|
27 |
} |
|
28 |
|
|
29 |
DefaultApiClient = new ApiClient( |
|
30 |
Config.ApiBaseAddress, |
|
31 |
Config.ApiPort, |
|
32 |
Config.ApiUsbEndPoint, |
|
33 |
Config.RetryPeriod, Config.MaxEntries, |
|
34 |
Config.MaxRetries, |
|
35 |
Config.CacheFileName |
|
36 |
); |
|
37 |
|
|
38 |
DefaultLogger.Debug("Main -> starting the ApiClient"); |
|
26 | 39 |
var apiClientThread = new Thread(DefaultApiClient.Run) { |
27 | 40 |
IsBackground = true |
28 | 41 |
}; |
29 |
DefaultLogger.Debug("Main -> starting the ApiClient"); |
|
30 | 42 |
apiClientThread.Start(); |
31 | 43 |
|
32 | 44 |
var admin = IsAdministrator(); |
33 | 45 |
DefaultLogger.Debug($"Is program executed with admin rights? {admin}"); |
34 | 46 |
|
35 |
var networkTread = new Thread(_netDetect.RunPeriodicDetection);
|
|
47 |
var networkTread = new Thread(NetDetect.RunPeriodicDetection);
|
|
36 | 48 |
networkTread.Start(); |
49 |
|
|
37 | 50 |
if (admin) { |
38 |
_procDetect.RegisterProcessListeners(); |
|
39 |
|
|
51 |
ProcDetect.RegisterProcessListeners(); |
|
40 | 52 |
} else { |
41 |
var processThread = new Thread(_procDetect.RunPeriodicDetection);
|
|
53 |
var processThread = new Thread(ProcDetect.RunPeriodicDetection);
|
|
42 | 54 |
processThread.Start(); |
43 | 55 |
processThread.Join(); |
44 | 56 |
} |
... | ... | |
49 | 61 |
DefaultApiClient.Stop(); |
50 | 62 |
apiClientThread.Join(); |
51 | 63 |
DefaultLogger.Debug("Main -> finished"); |
64 |
|
|
65 |
return 0; |
|
52 | 66 |
} |
53 | 67 |
|
54 |
public static bool IsAdministrator() { |
|
55 |
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { |
|
56 |
var identity = WindowsIdentity.GetCurrent(); |
|
57 |
var principal = new WindowsPrincipal(identity); |
|
58 |
return principal.IsInRole(WindowsBuiltInRole.Administrator); |
|
59 |
|
|
60 |
} else { |
|
68 |
private static bool IsAdministrator() { |
|
69 |
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { |
|
61 | 70 |
return false; |
62 | 71 |
} |
72 |
var identity = WindowsIdentity.GetCurrent(); |
|
73 |
var principal = new WindowsPrincipal(identity); |
|
74 |
return principal.IsInRole(WindowsBuiltInRole.Administrator); |
|
63 | 75 |
} |
64 | 76 |
} |
Také k dispozici: Unified diff
re #9567 Made sure that there is only one running process of the application.