Revize 31b6e8b7
Přidáno uživatelem Jakub Šilhavý před téměř 3 roky(ů)
ld_client/LDClient/Program.cs | ||
---|---|---|
9 | 9 |
|
10 | 10 |
namespace LDClient; |
11 | 11 |
|
12 |
/// <summary> |
|
13 |
/// This class represents the main class of the application. |
|
14 |
/// </summary> |
|
12 | 15 |
internal static class Program { |
13 | 16 |
|
17 |
/// <summary> |
|
18 |
/// Sleep period of the main thread of the application. |
|
19 |
/// </summary> |
|
14 | 20 |
private const int MainLoopDelayMs = 30000; |
15 | 21 |
|
22 |
/// <summary> |
|
23 |
/// Instance of a config loader. |
|
24 |
/// </summary> |
|
16 | 25 |
public static ConfigLoader Config { get; } = new(); |
26 |
|
|
27 |
/// <summary> |
|
28 |
/// Default logger used throughout the application. |
|
29 |
/// </summary> |
|
17 | 30 |
public static ALogger DefaultLogger { get; } = ALogger.Current; |
31 |
|
|
32 |
/// <summary> |
|
33 |
/// Instance of an API client. |
|
34 |
/// </summary> |
|
18 | 35 |
private static IApiClient? DefaultApiClient { get; set; } |
19 | 36 |
|
37 |
/// <summary> |
|
38 |
/// Instance of an info fetcher. |
|
39 |
/// </summary> |
|
20 | 40 |
private static readonly InfoFetcher InfoFetcher = new( |
21 | 41 |
Config.FetchInfoMaxAttempts, |
22 | 42 |
Config.FetchInfoAttemptPeriod, |
... | ... | |
27 | 47 |
Config.T32RemWaitTimeoutMs |
28 | 48 |
); |
29 | 49 |
|
50 |
/// <summary> |
|
51 |
/// The main entry pint of the application. |
|
52 |
/// </summary> |
|
53 |
/// <returns>1 if something goes wrong, 0 otherwise.</returns> |
|
30 | 54 |
public static int Main() { |
55 |
// Make sure that there is only one running instance of this application. |
|
31 | 56 |
if (GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1) { |
32 | 57 |
DefaultLogger.Error("Another instance of the application is already running"); |
33 | 58 |
return 1; |
34 | 59 |
} |
35 | 60 |
|
61 |
// Create an instance of an API client with all |
|
62 |
// the appropriate parameters defined in the config file. |
|
36 | 63 |
DefaultApiClient = new ApiClient( |
37 | 64 |
Config.ApiBaseAddress, |
38 | 65 |
Config.ApiPort, |
... | ... | |
42 | 69 |
new PersistentQueue(Config.CacheFileName) |
43 | 70 |
); |
44 | 71 |
|
72 |
// Create a new process detector. |
|
45 | 73 |
IProcessDetection processProcessDetection = new ProcessDetection( |
46 | 74 |
Config.T32ProcessName, |
47 | 75 |
Config.DetectionPeriod, |
... | ... | |
50 | 78 |
new ProcessUtils() |
51 | 79 |
); |
52 | 80 |
|
81 |
// Create and start a new thread that periodically |
|
82 |
// resends filed payloads to the server. |
|
53 | 83 |
var apiClientThread = new Thread(DefaultApiClient.Run) { |
54 | 84 |
IsBackground = true |
55 | 85 |
}; |
56 | 86 |
apiClientThread.Start(); |
57 | 87 |
|
88 |
// Create and start a new thread that periodically checks |
|
89 |
// if the desired process is currently running or not. |
|
58 | 90 |
var processThread = new Thread(processProcessDetection.RunPeriodicDetection) { |
59 | 91 |
IsBackground = true |
60 | 92 |
}; |
61 | 93 |
processThread.Start(); |
62 | 94 |
|
95 |
// The main thread does "nothing" |
|
63 | 96 |
while (true) { |
64 | 97 |
Thread.Sleep(MainLoopDelayMs); |
65 | 98 |
} |
99 |
|
|
100 |
// The execution of the program should never reach this point. |
|
101 |
return 0; |
|
66 | 102 |
} |
67 | 103 |
} |
Také k dispozici: Unified diff
re #9570 Commented Program.cs