Revize e6a01bd8
Přidáno uživatelem Pultak před více než 2 roky(ů)
ld_client/LDClient/detection/ProcessUtils.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Diagnostics; |
|
4 |
using System.Linq; |
|
5 |
using System.Text; |
|
6 |
using System.Threading.Tasks; |
|
1 |
using System.Diagnostics; |
|
2 |
|
|
3 |
namespace LDClient.detection; |
|
4 |
|
|
5 |
/// <summary> |
|
6 |
/// This class implements the IProcessUtils interface. |
|
7 |
/// It implements methods that are used when dealing with processes. |
|
8 |
/// </summary> |
|
9 |
public class ProcessUtils : IProcessUtils { |
|
7 | 10 |
|
8 |
namespace LDClient.detection { |
|
9 |
|
|
10 | 11 |
/// <summary> |
11 |
/// This class implements the IProcessUtils interface. |
|
12 |
/// It implements methods that are used when dealing with processes. |
|
12 |
/// Checks if a process is running or not. |
|
13 | 13 |
/// </summary> |
14 |
public class ProcessUtils : IProcessUtils { |
|
15 |
|
|
16 |
/// <summary> |
|
17 |
/// Checks if a process is running or not. |
|
18 |
/// </summary> |
|
19 |
/// <param name="name">Name of the process</param> |
|
20 |
/// <returns>True, if the process is running. False otherwise.</returns> |
|
21 |
public bool IsProcessRunning(string name) { |
|
22 |
return Process.GetProcessesByName(name).Length > 0; |
|
23 |
} |
|
14 |
/// <param name="name">Name of the process</param> |
|
15 |
/// <returns>True, if the process is running. False otherwise.</returns> |
|
16 |
public bool IsProcessRunning(string name) { |
|
17 |
return Process.GetProcessesByName(name).Length > 0; |
|
18 |
} |
|
24 | 19 |
|
25 |
/// <summary>
|
|
26 |
/// Executes a new process (t32rem.exe) with arguments which are passed in
|
|
27 |
/// as a parameter of the method.
|
|
28 |
/// </summary>
|
|
29 |
/// <param name="fileName">Path to the .exe file</param>
|
|
30 |
/// <param name="argument">Arguments passed into the .exe file</param>
|
|
31 |
/// <param name="timeout">Timeout used when waiting for the process to terminate</param>
|
|
32 |
/// <param name="desiredExitCode">Status code indicating a successful termination of the process.</param>
|
|
33 |
/// <returns>True, if the command was executed successfully. False otherwise.</returns>
|
|
34 |
public bool ExecuteNewProcess(string fileName, string argument, int timeout, int desiredExitCode) {
|
|
35 |
// Create a new process.
|
|
36 |
var t32RemProcess = new Process();
|
|
37 |
t32RemProcess.StartInfo.FileName = fileName;
|
|
38 |
t32RemProcess.StartInfo.Arguments = argument;
|
|
20 |
/// <summary> |
|
21 |
/// Executes a new process (t32rem.exe) with arguments which are passed in |
|
22 |
/// as a parameter of the method. |
|
23 |
/// </summary> |
|
24 |
/// <param name="fileName">Path to the .exe file</param> |
|
25 |
/// <param name="argument">Arguments passed into the .exe file</param> |
|
26 |
/// <param name="timeout">Timeout used when waiting for the process to terminate</param> |
|
27 |
/// <param name="desiredExitCode">Status code indicating a successful termination of the process.</param> |
|
28 |
/// <returns>True, if the command was executed successfully. False otherwise.</returns> |
|
29 |
public bool ExecuteNewProcess(string fileName, string argument, int timeout, int desiredExitCode) { |
|
30 |
// Create a new process. |
|
31 |
var t32RemProcess = new Process(); |
|
32 |
t32RemProcess.StartInfo.FileName = fileName; |
|
33 |
t32RemProcess.StartInfo.Arguments = argument; |
|
39 | 34 |
|
40 |
try {
|
|
41 |
// Execute the process and wait until it terminates or until the timeout is up.
|
|
42 |
t32RemProcess.Start();
|
|
43 |
if (!t32RemProcess.WaitForExit(timeout)) {
|
|
44 |
Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {timeout} ms");
|
|
45 |
return false;
|
|
46 |
}
|
|
35 |
try { |
|
36 |
// Execute the process and wait until it terminates or until the timeout is up. |
|
37 |
t32RemProcess.Start(); |
|
38 |
if (!t32RemProcess.WaitForExit(timeout)) { |
|
39 |
Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {timeout} ms"); |
|
40 |
return false; |
|
41 |
} |
|
47 | 42 |
|
48 |
// Check if the process terminated successfully. |
|
49 |
if (t32RemProcess.ExitCode != desiredExitCode) { |
|
50 |
Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}"); |
|
51 |
return false; |
|
52 |
} |
|
53 |
} catch (Exception exception) { |
|
54 |
Program.DefaultLogger.Error($"Failed to run {fileName} {argument}. {exception.Message}"); |
|
43 |
// Check if the process terminated successfully. |
|
44 |
if (t32RemProcess.ExitCode != desiredExitCode) { |
|
45 |
Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}"); |
|
55 | 46 |
return false; |
56 | 47 |
} |
57 |
return true; |
|
48 |
} catch (Exception exception) { |
|
49 |
Program.DefaultLogger.Error($"Failed to run {fileName} {argument}. {exception.Message}"); |
|
50 |
return false; |
|
58 | 51 |
} |
52 |
return true; |
|
59 | 53 |
} |
60 |
} |
|
54 |
} |
Také k dispozici: Unified diff
re #9712 Added documentation of the newly implemented methods + refactoring