@@ -11,36 +11,18 @@ public class ExternalProcess
1111 public int ExecuteSync ( string toolPath , string arguments )
1212 {
1313 ClearLog ( ) ;
14- var process = StartProcess ( toolPath , arguments ) ;
15-
16- if ( process == null )
17- {
18- return - 1 ;
19- }
20-
21- WaitForExit ( process ) ;
22-
23- return process . ExitCode ;
14+ return RunProcess ( toolPath , arguments ) ;
2415 }
2516
26- public async Task < int > ExecuteAsync ( string toolPath , string arguments )
17+ public Task < int > ExecuteAsync ( string toolPath , string arguments )
2718 {
2819 ClearLog ( ) ;
29- var process = StartProcess ( toolPath , arguments ) ;
30-
31- if ( process == null )
32- {
33- return - 1 ;
34- }
35-
36- await Task . Run ( ( ) => WaitForExit ( process ) ) ;
37-
38- return process . ExitCode ;
20+ return Task . Run ( ( ) => RunProcess ( toolPath , arguments ) ) ;
3921 }
4022
41- private Process StartProcess ( string toolPath , string arguments )
23+ private int RunProcess ( string toolPath , string arguments )
4224 {
43- var process = new Process ( ) ;
25+ Process process = new Process ( ) ;
4426
4527 process . StartInfo . FileName = toolPath ;
4628 process . StartInfo . Arguments = arguments ;
@@ -50,36 +32,38 @@ private Process StartProcess(string toolPath, string arguments)
5032 process . StartInfo . CreateNoWindow = true ;
5133 process . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
5234
35+ process . ErrorDataReceived += ( sender , errorLine ) =>
36+ {
37+ if ( errorLine . Data != null )
38+ {
39+ OutputLine ( errorLine . Data ) ;
40+ }
41+ } ;
42+ process . OutputDataReceived += ( sender , outputLine ) =>
43+ {
44+ if ( outputLine . Data != null )
45+ {
46+ OutputLine ( outputLine . Data ) ;
47+ }
48+ } ;
49+
5350 try
5451 {
5552 process . Start ( ) ;
53+ process . BeginErrorReadLine ( ) ;
54+ process . BeginOutputReadLine ( ) ;
55+ process . WaitForExit ( ) ;
5656 }
5757 catch ( Exception error )
5858 {
5959 OutputLine ( error . Message ) ;
60- return null ;
60+ return - 1 ;
6161 }
6262
63- return process ;
64- }
65-
66- private void WaitForExit ( Process process )
67- {
68- //Handle output
69- while ( ! process . StandardOutput . EndOfStream || ! process . StandardError . EndOfStream )
70- {
71- if ( ! process . StandardOutput . EndOfStream )
72- {
73- OutputLine ( process . StandardOutput . ReadLine ( ) ) ;
74- }
75-
76- if ( ! process . StandardError . EndOfStream )
77- {
78- OutputLine ( process . StandardError . ReadLine ( ) ) ;
79- }
80- }
63+ int exitCode = process . ExitCode ;
64+ process . Close ( ) ;
8165
82- process . WaitForExit ( ) ;
66+ return exitCode ;
8367 }
8468
8569 private void OutputLine ( string str )
0 commit comments