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.
This commit is contained in:
Scott Xu
2024-02-21 15:48:02 +08:00
committed by GitHub
parent 442f0a9706
commit b3ec023052
+12
View File
@@ -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)