解决方案 »

  1.   

     这是我的退出事件 private void btn_StopListenServer_Click(object sender, EventArgs e)
            {
                try
                {
                    SendMsgSocketThread.Abort();//线程终止
                    SendMsgSocket.Close();//关闭socket                SendStreamSocketThread.Abort();//线程终止
                    SendStreamSocket.Close();//关闭socket                GetMsgSocketThread.Abort();//线程终止
                    GetMsgSocket.Close();//关闭socket                GetStreamSocketThread.Abort();//线程终止
                    GetStreamSocket.Close();//关闭socket                //SendMsgToFlashThread.Abort();
                    //SendStreamToFlashThread.Abort();                this.btn_StartListenServer.Enabled = true;
                    this.btn_StopListenServer.Enabled = false;                this.listBox_ListenServer.Items.Clear();
                    this.listBox_ListenServer.Items.Add("等待运行..." + "     " + "服务于 " + DateTime.Now.ToString() + " 停止运行.");
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.Message);
                    lock (this.listBox_SendMsgClient)
                    {
                        this.listBox_SendMsgClient.Items.Clear();
                    }
                    lock (this.listBox_SendStreamClient)
                    {
                        this.listBox_SendStreamClient.Items.Clear();
                    }
                }        }
      

  2.   

    因为你的while循环退步出来,你可以做个标志,判断是否要结束线程,while里面判断,如果是,就退出
      

  3.   

    当设置了t.IsBackground = true时,就表明这个线程是后台线程,如果在前台再调用 t.Abort() 会出现异常的
      

  4.   

    但是我的while死循环是放在一个thread中的,结束按钮是放在主线程中的,我觉得两个线程是独立运行的,为什么控制图形界面的主线程也卡住了呢?
      

  5.   

    您好,请问能不能把您的socket开始监听部分代码发一份呢?
      

  6.   

    楼主,你根本没理解finally的意义,你在那里面搞了个侦听,回去好好看书吧。这么做当然要死机了。dispose()被你用死循环限制了,释放不掉了。
      

  7.   


        '停止监听线程
        Public Sub stopThreads()
            Try
                ListenLoop = False    '主监听线程停止循环标志
                listener.Close()      '立即中断主线程循环
            Catch
            End Try
       
            Try
                listener = Nothing
                MainThread.Abort()    '中止主监听线程
                MainThread.Join(300)  '等待300毫秒,超时也终止
            Catch
            End Try
        End Sub
      

  8.   

    在找问题之前,建议先把代码整理下
    把socket初始化、bind及listen的操作放在线程外。
    把accept的处理放在线程中去,
    线程中止,除非没有办法,否则别用Abort,
    目前的操作,点abort,肯定抛异常出来,
    界面肯定也会卡
    要让线程自己执行完毕自然推出。