Explorar el Código

Add `Closed` event to `ShellStream`. (#1332)

* Add `Closed` event to `ShellStream`. Lib consumer could hook to this event to detect if channel is closed by server **in time**.

* handle `Closed` event on different thread. No need to block original thread.
Scott Xu hace 1 año
padre
commit
b3ec023052
Se han modificado 1 ficheros con 12 adiciones y 0 borrados
  1. 12 0
      src/Renci.SshNet/ShellStream.cs

+ 12 - 0
src/Renci.SshNet/ShellStream.cs

@@ -8,6 +8,7 @@ using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading.Tasks;
 
+using Renci.SshNet.Abstractions;
 using Renci.SshNet.Channels;
 using Renci.SshNet.Common;
 
@@ -44,6 +45,11 @@ namespace Renci.SshNet
         /// </summary>
         public event EventHandler<ExceptionEventArgs>? ErrorOccurred;
 
+        /// <summary>
+        /// Occurs when the channel was closed.
+        /// </summary>
+        public event EventHandler<EventArgs>? Closed;
+
         /// <summary>
         /// Gets a value indicating whether data is available on the <see cref="ShellStream"/> to be read.
         /// </summary>
@@ -894,6 +900,12 @@ namespace Renci.SshNet
         private void Channel_Closed(object? sender, ChannelEventArgs e)
         {
             Dispose();
+
+            if (Closed != null)
+            {
+                // Handle event on different thread
+                ThreadAbstraction.ExecuteThread(() => Closed?.Invoke(this, EventArgs.Empty));
+            }
         }
 
         private void Channel_DataReceived(object? sender, ChannelDataEventArgs e)