@@ -56,6 +56,7 @@ 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 > ( ) ;
59
60
60
61
public Process Process { get ; }
61
62
public StreamWriter Input { get ; private set ; }
@@ -77,6 +78,24 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
77
78
78
79
public void Run ( )
79
80
{
81
+ if ( Process . StartInfo . RedirectStandardError )
82
+ {
83
+ Process . ErrorDataReceived += ( s , e ) =>
84
+ {
85
+ //if (e.Data != null)
86
+ //{
87
+ // Logger.Trace("ErrorData \"" + (e.Data == null ? "'null'" : e.Data) + "\"");
88
+ //}
89
+
90
+ string encodedData = null ;
91
+ if ( e . Data != null )
92
+ {
93
+ encodedData = Encoding . UTF8 . GetString ( Encoding . Default . GetBytes ( e . Data ) ) ;
94
+ errors . Add ( encodedData ) ;
95
+ }
96
+ } ;
97
+ }
98
+
80
99
try
81
100
{
82
101
Process . Start ( ) ;
@@ -101,60 +120,48 @@ public void Run()
101
120
102
121
if ( Process . StartInfo . RedirectStandardInput )
103
122
Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
104
-
105
- var errors = new List < string > ( ) ;
123
+ if ( Process . StartInfo . RedirectStandardError )
124
+ Process . BeginErrorReadLine ( ) ;
106
125
107
126
onStart ? . Invoke ( ) ;
108
- if ( Process . StartInfo . CreateNoWindow )
127
+
128
+ if ( Process . StartInfo . RedirectStandardOutput )
109
129
{
110
- if ( Process . StartInfo . RedirectStandardOutput )
130
+ var outputStream = Process . StandardOutput ;
131
+ var line = outputStream . ReadLine ( ) ;
132
+ while ( line != null )
111
133
{
112
- var outputStream = Process . StandardOutput ;
113
- var line = outputStream . ReadLine ( ) ;
114
- while ( line != null )
115
- {
116
- outputProcessor . LineReceived ( line ) ;
117
-
118
- if ( token . IsCancellationRequested )
119
- {
120
- if ( ! Process . HasExited )
121
- Process . Kill ( ) ;
134
+ outputProcessor . LineReceived ( line ) ;
122
135
123
- Process . Close ( ) ;
124
- onEnd ? . Invoke ( ) ;
125
- token . ThrowIfCancellationRequested ( ) ;
126
- }
136
+ if ( token . IsCancellationRequested )
137
+ {
138
+ if ( ! Process . HasExited )
139
+ Process . Kill ( ) ;
127
140
128
- line = outputStream . ReadLine ( ) ;
141
+ Process . Close ( ) ;
142
+ onEnd ? . Invoke ( ) ;
143
+ token . ThrowIfCancellationRequested ( ) ;
129
144
}
130
- outputProcessor . LineReceived ( null ) ;
145
+
146
+ line = outputStream . ReadLine ( ) ;
131
147
}
148
+ outputProcessor . LineReceived ( null ) ;
149
+ }
132
150
133
- if ( Process . StartInfo . RedirectStandardError )
151
+ if ( Process . StartInfo . CreateNoWindow )
152
+ {
153
+ while ( ! WaitForExit ( 500 ) )
134
154
{
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
- }
155
+ if ( token . IsCancellationRequested )
156
+ Process . Kill ( ) ;
157
+ Process . Close ( ) ;
158
+ onEnd ? . Invoke ( ) ;
159
+ token . ThrowIfCancellationRequested ( ) ;
160
+ }
153
161
154
- if ( Process . ExitCode != 0 && errors . Count > 0 )
155
- {
156
- onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
157
- }
162
+ if ( Process . ExitCode != 0 && errors . Count > 0 )
163
+ {
164
+ onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
158
165
}
159
166
}
160
167
0 commit comments