如果sshClient.RunCommand是同步的,你没有办法在它执行的时候终止它。改写成异步试试看。

解决方案 »

  1.   

    sshClient.RunCommand这个方法 里面如果有多线程调用的 那就直接修改咯 比如 里面有个thread:
    // Main thread:
    private static ManualResetEvent event = new ManualResetEvent(true); // boolean parameter whether to set the initial state to signaled.public void OnPauseClick(...) {
       event.Reset();
    }public void OnResumeClick(...) {
       event.Set();
    }// Worker thread
    public void DoSth() {
       while (true) {
         // show some random text in a label control (btw. you have to invoke the action because you are not in the main thread)
         event.WaitOne(); // waits for the signal to be set
       }
    }
      

  2.   

    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            int i = 0;
            private static ManualResetEvent manualEvent = new ManualResetEvent(true); // boolean parameter whether to set the initial state to signaled.
            private void start_Click(object sender, EventArgs e)
            {            ThreadPool.QueueUserWorkItem((state) =>
                {
                    while (true)
                    {
                        MessageBox.Show((i++).ToString());
                        manualEvent.WaitOne();
                    }
                });        }        public void OnPauseClick()
            {
                manualEvent.Reset();
            }        public void OnResumeClick()
            {
                manualEvent.Set();
            }        private void resume_Click(object sender, EventArgs e)
            {
                OnResumeClick();
            }        private void pause_Click(object sender, EventArgs e)
            {
                OnPauseClick();
            }
        }
    }
      

  3.   

    改成异步后怎样才能停止?不是很明白改成异步之后你不用做任何“停止”动作,只要 sshClient 对象被GC回收时,自然就停止处理连接了。