@@ -78,37 +78,19 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
7878
7979 public void Run ( )
8080 {
81- if ( Process . StartInfo . RedirectStandardOutput )
81+ if ( ! Process . StartInfo . RedirectStandardOutput )
8282 {
83- Process . OutputDataReceived += ( s , e ) =>
84- {
85- //Logger.Trace("OutputData \"" + (e.Data == null ? "'null'" : e.Data) + "\"");
86-
87- string encodedData = null ;
88- if ( e . Data != null )
89- {
90- encodedData = Encoding . UTF8 . GetString ( Encoding . Default . GetBytes ( e . Data ) ) ;
91- }
92- outputProcessor . LineReceived ( encodedData ) ;
93- } ;
83+ throw new ArgumentException ( "Process must RedirectStandardOutput" ) ;
9484 }
9585
96- if ( Process . StartInfo . RedirectStandardError )
86+ if ( ! Process . StartInfo . RedirectStandardError )
9787 {
98- Process . ErrorDataReceived += ( s , e ) =>
99- {
100- //if (e.Data != null)
101- //{
102- // Logger.Trace("ErrorData \"" + (e.Data == null ? "'null'" : e.Data) + "\"");
103- //}
88+ throw new ArgumentException ( "Process must RedirectStandardError" ) ;
89+ }
10490
105- string encodedData = null ;
106- if ( e . Data != null )
107- {
108- encodedData = Encoding . UTF8 . GetString ( Encoding . Default . GetBytes ( e . Data ) ) ;
109- errors . Add ( encodedData ) ;
110- }
111- } ;
91+ if ( ! Process . StartInfo . CreateNoWindow )
92+ {
93+ throw new ArgumentException ( "Process must CreateNoWindow" ) ;
11294 }
11395
11496 try
@@ -133,34 +115,55 @@ public void Run()
133115 return ;
134116 }
135117
136- if ( Process . StartInfo . RedirectStandardOutput )
137- Process . BeginOutputReadLine ( ) ;
138- if ( Process . StartInfo . RedirectStandardError )
139- Process . BeginErrorReadLine ( ) ;
140118 if ( Process . StartInfo . RedirectStandardInput )
141119 Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
142120
143121 onStart ? . Invoke ( ) ;
144122
145- if ( Process . StartInfo . CreateNoWindow )
123+ var outputStream = Process . StandardOutput ;
124+ var line = outputStream . ReadLine ( ) ;
125+ while ( line != null )
146126 {
147- while ( ! WaitForExit ( 500 ) )
127+ outputProcessor . LineReceived ( line ) ;
128+
129+ if ( token . IsCancellationRequested )
148130 {
149- if ( token . IsCancellationRequested )
150- {
151- if ( ! Process . HasExited )
152- Process . Kill ( ) ;
153- Process . Close ( ) ;
154- onEnd ? . Invoke ( ) ;
155- token . ThrowIfCancellationRequested ( ) ;
156- }
131+ if ( ! Process . HasExited )
132+ Process . Kill ( ) ;
133+
134+ Process . Close ( ) ;
135+ onEnd ? . Invoke ( ) ;
136+ token . ThrowIfCancellationRequested ( ) ;
157137 }
158138
159- if ( Process . ExitCode != 0 && errors . Count > 0 )
139+ line = outputStream . ReadLine ( ) ;
140+ }
141+ outputProcessor . LineReceived ( null ) ;
142+
143+ var errorStream = Process . StandardError ;
144+ var errorLine = errorStream . ReadLine ( ) ;
145+ while ( errorLine != null )
146+ {
147+ errors . Add ( errorLine ) ;
148+
149+ if ( token . IsCancellationRequested )
160150 {
161- onError ? . Invoke ( null , String . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
151+ if ( ! Process . HasExited )
152+ Process . Kill ( ) ;
153+
154+ Process . Close ( ) ;
155+ onEnd ? . Invoke ( ) ;
156+ token . ThrowIfCancellationRequested ( ) ;
162157 }
158+
159+ errorLine = errorStream . ReadLine ( ) ;
163160 }
161+
162+ if ( Process . ExitCode != 0 && errors . Count > 0 )
163+ {
164+ onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
165+ }
166+
164167 onEnd ? . Invoke ( ) ;
165168 }
166169
0 commit comments