Skip to content

Commit 2adaa5b

Browse files
committed
💾 Feat(CommandsExecutor): Now available to use CancellationToken to kill process.
1 parent d9d8b3d commit 2adaa5b

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎Common.BasicHelper/Core/Shell/CommandsExecutor.cs‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static async Task<string> GetExecutionResultAsync
6767
string args,
6868
bool findInPath = false,
6969
Action<ProcessStartInfo>? action = null,
70-
CancellationToken? token = default
70+
CancellationToken? token = null
7171
)
7272
{
7373
if (findInPath)
@@ -90,19 +90,24 @@ public static async Task<string> GetExecutionResultAsync
9090
StartInfo = psi,
9191
};
9292

93-
//var sb = new StringBuilder();
94-
95-
//void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
96-
// => sb.AppendLine(outLine.Data);
97-
98-
//process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
99-
//process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
100-
10193
process.Start();
10294

10395
var output = process.StandardOutput.ReadToEnd();
10496

105-
await Task.Run(process.WaitForExit);
97+
await Task.Run(() =>
98+
{
99+
if (token is null) process.WaitForExit();
100+
101+
while (!process.HasExited)
102+
{
103+
if (token is not null && token.Value.IsCancellationRequested)
104+
{
105+
process.Kill();
106+
107+
break;
108+
}
109+
}
110+
});
106111

107112
return output;
108113
}

0 commit comments

Comments
 (0)