How to cancel the ssh shell

Mar 4, 2015 at 4:03 AM
i have completed the shell function ,and now , i need add a new function that cancel the shell while shell is running.
                client = new SshClient(ip,user,password);
                client.ConnectionInfo.Encoding = ASCIIEncoding.Default;
                client.Connect();
                shells = client.CreateShellStream("test", 80, 24, 800, 600, 1024);
                shells.DataReceived += new EventHandler<Renci.SshNet.Common.ShellDataEventArgs>(shells_DataReceived);
                shells.Expect(new Regex(@"\[.*@.*\][\$|\#]"));
                shells.WriteLine(input);
                shells.Expect(new Regex(@"\[.*@.*\][\$|\#]"));
and
        public void Stop()
        {
            if (client != null)
                if (client.IsConnected)
                {
                    shells.Close();

                    client.Disconnect();
                }
        }
while the function"Stop()" used,the shell output stops,but the last sentence " shells.Expect(new Regex(@"\[.*@.*\][\$|\#]"));" does not work.

then ,my target is when i use the "Stop()" function,the ssh shell process will be closed,and throw an exception.
Thanks.