@@ -103,25 +103,25 @@ public static void RunProcess(string fileName, string arguments, string workingD
103
103
CreateNoWindow = true ,
104
104
UseShellExecute = false ,
105
105
WorkingDirectory = workingDirectory ,
106
- FileName = fileName ,
106
+ RedirectStandardOutput = true ,
107
107
RedirectStandardError = true ,
108
- RedirectStandardOutput = true
108
+ FileName = fileName
109
109
} ;
110
110
111
111
if ( ! string . IsNullOrEmpty ( arguments ) )
112
112
{
113
113
startInfo . Arguments = arguments ;
114
114
}
115
115
116
- Process testProcess = Process . Start ( startInfo ) ;
117
- string standardOut = null ;
118
- string standardError = null ;
116
+ Process testProcess = new ( )
117
+ {
118
+ StartInfo = startInfo ,
119
+ } ;
119
120
120
121
testProcess . OutputDataReceived += ( sender , e ) =>
121
122
{
122
123
if ( e . Data != null )
123
124
{
124
- standardOut = e . Data + Environment . NewLine ;
125
125
writeOutput ? . Invoke ( e . Data ) ;
126
126
}
127
127
} ;
@@ -130,31 +130,35 @@ public static void RunProcess(string fileName, string arguments, string workingD
130
130
{
131
131
if ( e . Data != null )
132
132
{
133
- standardOut += e . Data + Environment . NewLine ;
134
133
writeError ? . Invoke ( e . Data ) ;
135
134
}
136
135
} ;
137
136
138
- bool completed = testProcess . WaitForExit ( ( int ) procTimeout . TotalMilliseconds ) ;
137
+ testProcess . Start ( ) ;
138
+ testProcess . BeginOutputReadLine ( ) ;
139
+ testProcess . BeginErrorReadLine ( ) ;
139
140
140
- if ( ! completed )
141
+ bool completed = false ;
142
+ try
141
143
{
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 } '.") ;
144
149
}
145
150
146
- if ( testProcess . ExitCode != 0 )
151
+ if ( ! completed )
147
152
{
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 } .") ;
150
154
}
151
155
}
152
156
153
157
private static string ExecuteCommand ( string command )
154
158
{
155
- string output = null ;
159
+ string output = string . Empty ;
156
160
157
- RunProcess ( CommandExe , command , null , writeOutput : o => output = o ) ;
161
+ RunProcess ( CommandExe , command , null , writeOutput : o => output + = o + Environment . NewLine ) ;
158
162
159
163
return output ;
160
164
}
0 commit comments