@@ -56,7 +56,6 @@ class ProcessWrapper
56
56
private readonly Action onEnd ;
57
57
private readonly Action < Exception , string > onError ;
58
58
private readonly CancellationToken token ;
59
- private readonly List < string > errors = new List < string > ( ) ;
60
59
61
60
public Process Process { get ; }
62
61
public StreamWriter Input { get ; private set ; }
@@ -78,39 +77,6 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
78
77
79
78
public void Run ( )
80
79
{
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
-
114
80
try
115
81
{
116
82
Process . Start ( ) ;
@@ -133,34 +99,65 @@ public void Run()
133
99
return ;
134
100
}
135
101
136
- if ( Process . StartInfo . RedirectStandardOutput )
137
- Process . BeginOutputReadLine ( ) ;
138
- if ( Process . StartInfo . RedirectStandardError )
139
- Process . BeginErrorReadLine ( ) ;
140
102
if ( Process . StartInfo . RedirectStandardInput )
141
103
Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
142
104
143
- onStart ? . Invoke ( ) ;
105
+ var errors = new List < string > ( ) ;
144
106
107
+ onStart ? . Invoke ( ) ;
145
108
if ( Process . StartInfo . CreateNoWindow )
146
109
{
147
- while ( ! WaitForExit ( 500 ) )
110
+ if ( Process . StartInfo . RedirectStandardOutput )
148
111
{
149
- if ( token . IsCancellationRequested )
112
+ var outputStream = Process . StandardOutput ;
113
+ var line = outputStream . ReadLine ( ) ;
114
+ while ( line != null )
150
115
{
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 ( ) ;
156
129
}
130
+ outputProcessor . LineReceived ( null ) ;
157
131
}
158
132
159
- if ( Process . ExitCode != 0 && errors . Count > 0 )
133
+ if ( Process . StartInfo . RedirectStandardError )
160
134
{
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
+ }
162
158
}
163
159
}
160
+
164
161
onEnd ? . Invoke ( ) ;
165
162
}
166
163
0 commit comments