@@ -28,8 +28,8 @@ public class SshCommand : IDisposable
2828        private  EventWaitHandle  _sessionErrorOccuredWaitHandle ; 
2929        private  EventWaitHandle  _commandCancelledWaitHandle ; 
3030        private  Exception  _exception ; 
31-         private  StringBuilder  _result ; 
32-         private  StringBuilder  _error ; 
31+         private  string  _result ; 
32+         private  string  _error ; 
3333        private  bool  _hasError ; 
3434        private  bool  _isDisposed ; 
3535        private  bool  _isCancelled ; 
@@ -109,21 +109,22 @@ public string Result
109109        { 
110110            get 
111111            { 
112-                 _result  ??=  new  StringBuilder ( ) ; 
112+                 if  ( _result  is  not null ) 
113+                 { 
114+                     return  _result ; 
115+                 } 
113116
114-                 if  ( OutputStream  !=  null   &&   OutputStream . Length   >   0 ) 
117+                 if  ( OutputStream  is  null ) 
115118                { 
116-                     using  ( var  sr  =  new  StreamReader ( OutputStream , 
117-                                                      _encoding , 
118-                                                      detectEncodingFromByteOrderMarks :  true , 
119-                                                      bufferSize :  1024 , 
120-                                                      leaveOpen :  true ) ) 
121-                     { 
122-                         _  =  _result . Append ( sr . ReadToEnd ( ) ) ; 
123-                     } 
119+                     return  string . Empty ; 
124120                } 
125121
126-                 return  _result . ToString ( ) ; 
122+                 using  ( var  sr  =  new  StreamReader ( OutputStream , 
123+                                                  _encoding , 
124+                                                  detectEncodingFromByteOrderMarks :  true ) ) 
125+                 { 
126+                     return  _result  =  sr . ReadToEnd ( ) ; 
127+                 } 
127128            } 
128129        } 
129130
@@ -134,26 +135,22 @@ public string Error
134135        { 
135136            get 
136137            { 
137-                 if  ( _hasError ) 
138+                 if  ( _error  is  not null ) 
139+                 { 
140+                     return  _error ; 
141+                 } 
142+ 
143+                 if  ( ExtendedOutputStream  is  null  ||  ! _hasError ) 
138144                { 
139-                     _error  ??=  new  StringBuilder ( ) ; 
140- 
141-                     if  ( ExtendedOutputStream  !=  null  &&  ExtendedOutputStream . Length  >  0 ) 
142-                     { 
143-                         using  ( var  sr  =  new  StreamReader ( ExtendedOutputStream , 
144-                                                          _encoding , 
145-                                                          detectEncodingFromByteOrderMarks :  true , 
146-                                                          bufferSize :  1024 , 
147-                                                          leaveOpen :  true ) ) 
148-                         { 
149-                             _  =  _error . Append ( sr . ReadToEnd ( ) ) ; 
150-                         } 
151-                     } 
152- 
153-                     return  _error . ToString ( ) ; 
145+                     return  string . Empty ; 
154146                } 
155147
156-                 return  string . Empty ; 
148+                 using  ( var  sr  =  new  StreamReader ( ExtendedOutputStream , 
149+                                                  _encoding , 
150+                                                  detectEncodingFromByteOrderMarks :  true ) ) 
151+                 { 
152+                     return  _error  =  sr . ReadToEnd ( ) ; 
153+                 } 
157154            } 
158155        } 
159156
@@ -265,26 +262,16 @@ public IAsyncResult BeginExecute(AsyncCallback callback, object state)
265262                throw  new  ArgumentException ( "CommandText property is empty." ) ; 
266263            } 
267264
268-             var  outputStream  =  OutputStream ; 
269-             if  ( outputStream  is  not null ) 
270-             { 
271-                 outputStream . Dispose ( ) ; 
272-                 OutputStream  =  null ; 
273-             } 
274- 
275-             var  extendedOutputStream  =  ExtendedOutputStream ; 
276-             if  ( extendedOutputStream  is  not null ) 
277-             { 
278-                 extendedOutputStream . Dispose ( ) ; 
279-                 ExtendedOutputStream  =  null ; 
280-             } 
265+             OutputStream ? . Dispose ( ) ; 
266+             ExtendedOutputStream ? . Dispose ( ) ; 
281267
282268            // Initialize output streams 
283269            OutputStream  =  new  PipeStream ( ) ; 
284270            ExtendedOutputStream  =  new  PipeStream ( ) ; 
285271
286272            _result  =  null ; 
287273            _error  =  null ; 
274+             _hasError  =  false ; 
288275            _callback  =  callback ; 
289276
290277            _channel  =  CreateChannel ( ) ; 
@@ -341,13 +328,21 @@ public string EndExecute(IAsyncResult asyncResult)
341328
342329                _inputStream ? . Close ( ) ; 
343330
344-                 // wait for operation to complete (or time out) 
345-                 WaitOnHandle ( _asyncResult . AsyncWaitHandle ) ; 
331+                 try 
332+                 { 
333+                     // wait for operation to complete (or time out) 
334+                     WaitOnHandle ( _asyncResult . AsyncWaitHandle ) ; 
335+                 } 
336+                 finally 
337+                 { 
338+                     UnsubscribeFromEventsAndDisposeChannel ( _channel ) ; 
339+                     _channel  =  null ; 
346340
347-                 UnsubscribeFromEventsAndDisposeChannel ( _channel ) ; 
348-                 _channel   =   null ; 
341+                      OutputStream ? . Dispose ( ) ; 
342+                      ExtendedOutputStream ? . Dispose ( ) ; 
349343
350-                 commandAsyncResult . EndCalled  =  true ; 
344+                     commandAsyncResult . EndCalled  =  true ; 
345+                 } 
351346
352347                if  ( ! _isCancelled ) 
353348                { 
@@ -437,8 +432,8 @@ private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
437432
438433        private  void  SetAsyncComplete ( ) 
439434        { 
440-             OutputStream ? . Flush ( ) ; 
441-             ExtendedOutputStream ? . Flush ( ) ; 
435+             OutputStream ? . Dispose ( ) ; 
436+             ExtendedOutputStream ? . Dispose ( ) ; 
442437
443438            _asyncResult . IsCompleted  =  true ; 
444439
@@ -480,11 +475,7 @@ private void Channel_RequestReceived(object sender, ChannelRequestEventArgs e)
480475
481476        private  void  Channel_ExtendedDataReceived ( object  sender ,  ChannelExtendedDataEventArgs  e ) 
482477        { 
483-             if  ( ExtendedOutputStream  !=  null ) 
484-             { 
485-                 ExtendedOutputStream . Write ( e . Data ,  0 ,  e . Data . Length ) ; 
486-                 ExtendedOutputStream . Flush ( ) ; 
487-             } 
478+             ExtendedOutputStream ? . Write ( e . Data ,  0 ,  e . Data . Length ) ; 
488479
489480            if  ( e . DataTypeCode  ==  1 ) 
490481            { 
@@ -494,11 +485,7 @@ private void Channel_ExtendedDataReceived(object sender, ChannelExtendedDataEven
494485
495486        private  void  Channel_DataReceived ( object  sender ,  ChannelDataEventArgs  e ) 
496487        { 
497-             if  ( OutputStream  !=  null ) 
498-             { 
499-                 OutputStream . Write ( e . Data ,  0 ,  e . Data . Length ) ; 
500-                 OutputStream . Flush ( ) ; 
501-             } 
488+             OutputStream ? . Write ( e . Data ,  0 ,  e . Data . Length ) ; 
502489
503490            if  ( _asyncResult  !=  null ) 
504491            { 
0 commit comments