@@ -103,25 +103,25 @@ public static void RunProcess(string fileName, string arguments, string workingD
103103 CreateNoWindow = true ,
104104 UseShellExecute = false ,
105105 WorkingDirectory = workingDirectory ,
106- FileName = fileName ,
106+ RedirectStandardOutput = true ,
107107 RedirectStandardError = true ,
108- RedirectStandardOutput = true
108+ FileName = fileName
109109 } ;
110110
111111 if ( ! string . IsNullOrEmpty ( arguments ) )
112112 {
113113 startInfo . Arguments = arguments ;
114114 }
115115
116- Process testProcess = Process . Start ( startInfo ) ;
117- string standardOut = null ;
118- string standardError = null ;
116+ Process testProcess = new ( )
117+ {
118+ StartInfo = startInfo ,
119+ } ;
119120
120121 testProcess . OutputDataReceived += ( sender , e ) =>
121122 {
122123 if ( e . Data != null )
123124 {
124- standardOut = e . Data + Environment . NewLine ;
125125 writeOutput ? . Invoke ( e . Data ) ;
126126 }
127127 } ;
@@ -130,31 +130,35 @@ public static void RunProcess(string fileName, string arguments, string workingD
130130 {
131131 if ( e . Data != null )
132132 {
133- standardOut += e . Data + Environment . NewLine ;
134133 writeError ? . Invoke ( e . Data ) ;
135134 }
136135 } ;
137136
138- bool completed = testProcess . WaitForExit ( ( int ) procTimeout . TotalMilliseconds ) ;
137+ testProcess . Start ( ) ;
138+ testProcess . BeginOutputReadLine ( ) ;
139+ testProcess . BeginErrorReadLine ( ) ;
139140
140- if ( ! completed )
141+ bool completed = false ;
142+ try
141143 {
142- testProcess . Kill ( ) ;
143- throw new TimeoutException ( $ "Process '{ fileName } { arguments } ' in working directory '{ workingDirectory } ' did not complete in { procTimeout } .") ;
144+ completed = testProcess . WaitForExit ( ( int ) procTimeout . TotalMilliseconds ) ;
145+ }
146+ catch ( Exception ex )
147+ {
148+ Console . WriteLine ( $ "Process '{ fileName } { arguments } ' in working directory '{ workingDirectory } ' threw exception '{ ex } '.") ;
144149 }
145150
146- if ( testProcess . ExitCode != 0 )
151+ if ( ! completed )
147152 {
148- throw new InvalidOperationException ( $ "Process '{ fileName } { arguments } ' in working directory '{ workingDirectory } ' exited with code '{ testProcess . ExitCode } '.{ Environment . NewLine } " +
149- $ "Output:{ Environment . NewLine } { standardOut } { Environment . NewLine } Error:{ Environment . NewLine } { standardError } ") ;
153+ throw new TimeoutException ( $ "Process '{ fileName } { arguments } ' in working directory '{ workingDirectory } ' did not complete in { procTimeout } .") ;
150154 }
151155 }
152156
153157 private static string ExecuteCommand ( string command )
154158 {
155- string output = null ;
159+ string output = string . Empty ;
156160
157- RunProcess ( CommandExe , command , null , writeOutput : o => output = o ) ;
161+ RunProcess ( CommandExe , command , null , writeOutput : o => output + = o + Environment . NewLine ) ;
158162
159163 return output ;
160164 }
0 commit comments