WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
ProcessManager.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System.Diagnostics;
6using System.Text;
7using System.Threading;
8using System.Threading.Tasks;
10using WGetNET.Models;
11
13{
18 internal class ProcessManager
19 {
20 private readonly ProcessStartInfo _winGetStartInfoTemplate;
21
28 public ProcessManager(string processName)
29 {
30 _winGetStartInfoTemplate = new ProcessStartInfo()
31 {
32 CreateNoWindow = true,
33 FileName = processName,
34 UseShellExecute = false,
35 RedirectStandardOutput = true,
36 StandardOutputEncoding = Encoding.UTF8
37 };
38 }
39
50 public ProcessResult ExecuteWingetProcess(string cmd)
51 {
52 return RunProcess(GetStartInfo(cmd));
53 }
54
68 public async Task<ProcessResult> ExecuteWingetProcessAsync(string cmd, CancellationToken cancellationToken = default)
69 {
70 return await RunProcessAsync(GetStartInfo(cmd), cancellationToken);
71 }
72
82 private ProcessStartInfo GetStartInfo(string cmd)
83 {
84 return new ProcessStartInfo()
85 {
86 CreateNoWindow = _winGetStartInfoTemplate.CreateNoWindow,
87 FileName = _winGetStartInfoTemplate.FileName,
88 RedirectStandardOutput = _winGetStartInfoTemplate.RedirectStandardOutput,
89 StandardOutputEncoding = _winGetStartInfoTemplate.StandardOutputEncoding,
90 UseShellExecute = _winGetStartInfoTemplate.UseShellExecute,
91 WindowStyle = _winGetStartInfoTemplate.WindowStyle,
92 Arguments = cmd
93 };
94 }
95
106 private ProcessResult RunProcess(ProcessStartInfo processStartInfo)
107 {
108 ProcessResult result = new();
109
110 //Create and run process
111 using (Process proc = new() { StartInfo = processStartInfo })
112 {
113 proc.Start();
114
115 result.Output = proc.StandardOutput.ReadSreamOutputByLine();
116
117 //Wait till end and get exit code
118 proc.WaitForExit();
119
120 // Make sure the process has exited
121 if (!proc.HasExited)
122 {
123 proc.Kill();
124 }
125
126 result.ExitCode = proc.ExitCode;
127 }
128
129 return result;
130 }
131
145 private async Task<ProcessResult> RunProcessAsync(ProcessStartInfo processStartInfo, CancellationToken cancellationToken = default)
146 {
147 ProcessResult result = new();
148
149 //Create and run process
150 using (Process proc = new() { StartInfo = processStartInfo })
151 {
152 proc.Start();
153
154 result.Output = await proc.StandardOutput.ReadSreamOutputByLineAsync(cancellationToken);
155
156 // Kill the process and return, if the task is canceled
157 if (cancellationToken.IsCancellationRequested && !proc.HasExited)
158 {
159 proc.Kill();
160
161 result.ExitCode = -1;
162
163 return result;
164 }
165
166 //Wait for the processs to exit
167 proc.WaitForExit();
168
169 // Make sure the process has exited
170 if (!proc.HasExited)
171 {
172 proc.Kill();
173 }
174
175 result.ExitCode = proc.ExitCode;
176 }
177
178 return result;
179 }
180 }
181}