@@ -78,37 +78,19 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
78
78
79
79
public void Run ( )
80
80
{
81
- if ( Process . StartInfo . RedirectStandardOutput )
81
+ if ( ! Process . StartInfo . RedirectStandardOutput )
82
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
- } ;
83
+ throw new ArgumentException ( "Process must RedirectStandardOutput" ) ;
94
84
}
95
85
96
- if ( Process . StartInfo . RedirectStandardError )
86
+ if ( ! Process . StartInfo . RedirectStandardError )
97
87
{
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
+ }
104
90
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" ) ;
112
94
}
113
95
114
96
try
@@ -133,34 +115,55 @@ public void Run()
133
115
return ;
134
116
}
135
117
136
- if ( Process . StartInfo . RedirectStandardOutput )
137
- Process . BeginOutputReadLine ( ) ;
138
- if ( Process . StartInfo . RedirectStandardError )
139
- Process . BeginErrorReadLine ( ) ;
140
118
if ( Process . StartInfo . RedirectStandardInput )
141
119
Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
142
120
143
121
onStart ? . Invoke ( ) ;
144
122
145
- if ( Process . StartInfo . CreateNoWindow )
123
+ var outputStream = Process . StandardOutput ;
124
+ var line = outputStream . ReadLine ( ) ;
125
+ while ( line != null )
146
126
{
147
- while ( ! WaitForExit ( 500 ) )
127
+ outputProcessor . LineReceived ( line ) ;
128
+
129
+ if ( token . IsCancellationRequested )
148
130
{
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 ( ) ;
157
137
}
158
138
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 )
160
150
{
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 ( ) ;
162
157
}
158
+
159
+ errorLine = errorStream . ReadLine ( ) ;
163
160
}
161
+
162
+ if ( Process . ExitCode != 0 && errors . Count > 0 )
163
+ {
164
+ onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
165
+ }
166
+
164
167
onEnd ? . Invoke ( ) ;
165
168
}
166
169
0 commit comments