@@ -56,7 +56,6 @@ class ProcessWrapper
5656 private readonly Action onEnd ;
5757 private readonly Action < Exception , string > onError ;
5858 private readonly CancellationToken token ;
59- private readonly List < string > errors = new List < string > ( ) ;
6059
6160 public Process Process { get ; }
6261 public StreamWriter Input { get ; private set ; }
@@ -78,39 +77,6 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
7877
7978 public void Run ( )
8079 {
81- if ( Process . StartInfo . RedirectStandardOutput )
82- {
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- } ;
94- }
95-
96- if ( Process . StartInfo . RedirectStandardError )
97- {
98- Process . ErrorDataReceived += ( s , e ) =>
99- {
100- //if (e.Data != null)
101- //{
102- // Logger.Trace("ErrorData \"" + (e.Data == null ? "'null'" : e.Data) + "\"");
103- //}
104-
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- } ;
112- }
113-
11480 try
11581 {
11682 Process . Start ( ) ;
@@ -133,34 +99,65 @@ public void Run()
13399 return ;
134100 }
135101
136- if ( Process . StartInfo . RedirectStandardOutput )
137- Process . BeginOutputReadLine ( ) ;
138- if ( Process . StartInfo . RedirectStandardError )
139- Process . BeginErrorReadLine ( ) ;
140102 if ( Process . StartInfo . RedirectStandardInput )
141103 Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
142104
143- onStart ? . Invoke ( ) ;
105+ var errors = new List < string > ( ) ;
144106
107+ onStart ? . Invoke ( ) ;
145108 if ( Process . StartInfo . CreateNoWindow )
146109 {
147- while ( ! WaitForExit ( 500 ) )
110+ if ( Process . StartInfo . RedirectStandardOutput )
148111 {
149- if ( token . IsCancellationRequested )
112+ var outputStream = Process . StandardOutput ;
113+ var line = outputStream . ReadLine ( ) ;
114+ while ( line != null )
150115 {
151- if ( ! Process . HasExited )
152- Process . Kill ( ) ;
153- Process . Close ( ) ;
154- onEnd ? . Invoke ( ) ;
155- token . ThrowIfCancellationRequested ( ) ;
116+ outputProcessor . LineReceived ( line ) ;
117+
118+ if ( token . IsCancellationRequested )
119+ {
120+ if ( ! Process . HasExited )
121+ Process . Kill ( ) ;
122+
123+ Process . Close ( ) ;
124+ onEnd ? . Invoke ( ) ;
125+ token . ThrowIfCancellationRequested ( ) ;
126+ }
127+
128+ line = outputStream . ReadLine ( ) ;
156129 }
130+ outputProcessor . LineReceived ( null ) ;
157131 }
158132
159- if ( Process . ExitCode != 0 && errors . Count > 0 )
133+ if ( Process . StartInfo . RedirectStandardError )
160134 {
161- onError ? . Invoke ( null , String . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
135+ var errorStream = Process . StandardError ;
136+ var errorLine = errorStream . ReadLine ( ) ;
137+ while ( errorLine != null )
138+ {
139+ errors . Add ( errorLine ) ;
140+
141+ if ( token . IsCancellationRequested )
142+ {
143+ if ( ! Process . HasExited )
144+ Process . Kill ( ) ;
145+
146+ Process . Close ( ) ;
147+ onEnd ? . Invoke ( ) ;
148+ token . ThrowIfCancellationRequested ( ) ;
149+ }
150+
151+ errorLine = errorStream . ReadLine ( ) ;
152+ }
153+
154+ if ( Process . ExitCode != 0 && errors . Count > 0 )
155+ {
156+ onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
157+ }
162158 }
163159 }
160+
164161 onEnd ? . Invoke ( ) ;
165162 }
166163
0 commit comments