/// <summary> /// Stops this shell. /// </summary> /// <exception cref="SshException">Shell is not started.</exception> public void Stop() { if (!IsStarted) { throw new SshException("Shell is not started."); } if (_channel != null) { _channel.Close(); } }
private void Session_Disconnected(object sender, EventArgs e) { if (_channel != null) { _channel.Close(); } }
/// <summary> /// Waits for the pending asynchronous command execution to complete. /// </summary> /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> /// <returns>Command execution result.</returns> /// <example> /// <code source="..\..\TCMSSH.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateCommand BeginExecute IsCompleted EndExecute" language="C#" title="Asynchronous Command Execution" /> /// </example> /// <exception cref="ArgumentException">Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.</exception> public string EndExecute(IAsyncResult asyncResult) { if (this._asyncResult == asyncResult && this._asyncResult != null) { lock (this._endExecuteLock) { if (this._asyncResult != null) { // Make sure that operation completed if not wait for it to finish this.WaitOnHandle(this._asyncResult.AsyncWaitHandle); if (_channel.IsOpen) { _channel.Close(); } UnsubscribeFromEventsAndDisposeChannel(); _channel = null; _asyncResult = null; return(this.Result); } } } throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult."); }
/// <summary> /// Waits for the pending asynchronous command execution to complete. /// </summary> /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> /// <returns>Command execution result.</returns> /// <example> /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateCommand BeginExecute IsCompleted EndExecute" language="C#" title="Asynchronous Command Execution" /> /// </example> /// <exception cref="ArgumentException">Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.</exception> /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <c>null</c>.</exception> public string EndExecute(IAsyncResult asyncResult) { if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } var commandAsyncResult = asyncResult as CommandAsyncResult; if (commandAsyncResult == null || _asyncResult != commandAsyncResult) { throw new ArgumentException(string.Format("The {0} object was not returned from the corresponding asynchronous method on this class.", typeof(IAsyncResult).Name)); } lock (_endExecuteLock) { if (commandAsyncResult.EndCalled) { throw new ArgumentException("EndExecute can only be called once for each asynchronous operation."); } // wait for operation to complete (or time out) WaitOnHandle(_asyncResult.AsyncWaitHandle); if (_channel.IsOpen) { _channel.Close(); } UnsubscribeFromEventsAndDisposeChannel(_channel); _channel = null; commandAsyncResult.EndCalled = true; return(Result); } }
/// <summary> /// Disconnects the subsystem channel. /// </summary> public void Disconnect() { if (_session != null) { _session.ErrorOccured -= Session_ErrorOccured; _session.Disconnected -= Session_Disconnected; } if (_channel != null) { _channel.DataReceived -= Channel_DataReceived; _channel.Exception -= Channel_Exception; _channel.Closed -= Channel_Closed; _channel.Close(); _channel.Dispose(); _channel = null; } }