Projekt

Obecné

Profil

Stáhnout (2.6 KB) Statistiky
| Větev: | Tag: | Revize:
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;
7

    
8
namespace LDClient.detection {
9
    
10
    /// <summary>
11
    /// This class implements the IProcessUtils interface.
12
    /// It implements methods that are used when dealing with processes.
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
        }
24
        
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;
39
            
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
                }
47
                
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}");
55
                return false;
56
            }
57
            return true;
58
        }
59
    }
60
}
(7-7/7)