Renci.SshNet.ShellStream.Dispose C# (CSharp) Method

Dispose() protected method

Releases the unmanaged resources used by the T:System.IO.Stream and optionally releases the managed resources.
protected Dispose ( bool disposing ) : void
disposing bool true to release both managed and unmanaged resources; false to release only unmanaged resources.
return void
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (_isDisposed)
                return;

            if (disposing)
            {
                UnsubscribeFromSessionEvents(_session);

                if (_channel != null)
                {
                    _channel.DataReceived -= Channel_DataReceived;
                    _channel.Closed -= Channel_Closed;
                    _channel.Dispose();
                    _channel = null;
                }

                if (_dataReceived != null)
                {
                    _dataReceived.Dispose();
                    _dataReceived = null;
                }

                _isDisposed = true;
            }
            else
            {
                UnsubscribeFromSessionEvents(_session);
            }
        }

Usage Example

Beispiel #1
0
        public bool StartShellStream()
        {
            if (!ConnectSSH ())	return false;

            ManualResetEvent started = new ManualResetEvent(false);
            ShellStreamTask = new Task( () =>
            {
                try
                {
                    WriteLine("Starting terminal: Emulation = {0}",Host.TerminalEmulation);

                    shellStream = sshClient.CreateShellStream(Host.TerminalEmulation,(uint)Host.TerminalCols,(uint)Host.TerminalRows,0,0,4096);

                    shellStream.DataReceived += (object sender, ShellDataEventArgs e) =>
                    {
                        Write(Encoding.UTF8.GetString(e.Data,0,e.Data.Length));
                    };

                    shellStream.ErrorOccurred+= (object sender, ExceptionEventArgs e) =>
                    {
                        WriteLine(e.Exception.Message);
                        keepShellAlive.Set();
                    };

                    if (!String.IsNullOrEmpty(Host.WorkingDir))
                    {
                        Write("Changing dir: {0}...",Host.WorkingDir);
                        shellStream.WriteLine(String.Format("cd {0}\r\n",Host.WorkingDir));
                    }
                    started.Set();
                    keepShellAlive.Reset();
                    keepShellAlive.WaitOne();

                    WriteLine("\r\n*** Console Stream End");
                }
                catch (Exception ex)
                {
                    WriteLine("\r\n*** Console Stream Error: {0}",ex.Message);
                }
                finally
                {
                    shellStream.Dispose();
                    shellStream = null;
                }

            });
            ShellStreamTask.Start();
            return started.WaitOne(5000);
        }