@@ -5,32 +5,46 @@ namespace QtSharp
55{
66 public class ProcessHelper
77 {
8- public static string Run ( string path , string args , out string error , bool readOutputByLines = false , bool waitForExit = true )
8+ public static string Run ( string path , string args , out string error , bool readOutputByLines = false )
99 {
1010 try
1111 {
1212 using ( Process process = new Process ( ) )
1313 {
14+ Console . WriteLine ( "Run: " + path ) ;
15+ Console . WriteLine ( "Args: " + args ) ;
1416 process . StartInfo . FileName = path ;
1517 process . StartInfo . Arguments = args ;
1618 process . StartInfo . UseShellExecute = false ;
1719 process . StartInfo . RedirectStandardOutput = true ;
1820 process . StartInfo . RedirectStandardError = true ;
19- process . Start ( ) ;
20- if ( waitForExit )
21+
22+ var reterror = "" ;
23+ var retout = "" ;
24+ process . OutputDataReceived += ( sender , outargs ) =>
2125 {
22- process . WaitForExit ( ) ;
23- }
24- while ( readOutputByLines && ! process . StandardOutput . EndOfStream )
26+ if ( retout != "" && outargs . Data != "" ) retout += "\r \n " ;
27+ retout += outargs . Data ;
28+ Console . WriteLine ( "stdout: {0}" , retout ) ;
29+ } ;
30+ process . ErrorDataReceived += ( sender , errargs ) =>
2531 {
26- Console . WriteLine ( process . StandardOutput . ReadLine ( ) ) ;
27- }
28- error = process . StandardError . ReadToEnd ( ) ;
32+ if ( reterror != "" && errargs . Data != "" ) reterror += "\r \n " ;
33+ reterror += errargs . Data ;
34+ Console . WriteLine ( "stderr: {0}" , reterror ) ;
35+ } ;
36+
37+ process . Start ( ) ;
38+ process . BeginOutputReadLine ( ) ;
39+ process . BeginErrorReadLine ( ) ;
40+ process . WaitForExit ( ) ;
41+ process . CancelOutputRead ( ) ;
42+ process . CancelErrorRead ( ) ;
43+
44+ error = reterror ;
2945 if ( process . ExitCode != 0 )
30- {
31- return string . Empty ;
32- }
33- return readOutputByLines ? string . Empty : process . StandardOutput . ReadToEnd ( ) . Trim ( ) . Replace ( @"\\" , @"\" ) ;
46+ throw new Exception ( "Exit Code is not 0" ) ;
47+ return readOutputByLines ? string . Empty : retout . Trim ( ) . Replace ( @"\\" , @"\" ) ;
3448 }
3549 }
3650 catch ( Exception exception )
0 commit comments