C#采用线程监听socket连接出现界面卡死 socket线程界面界面卡死thread 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我的退出事件 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(); } } } 因为你的while循环退步出来,你可以做个标志,判断是否要结束线程,while里面判断,如果是,就退出 当设置了t.IsBackground = true时,就表明这个线程是后台线程,如果在前台再调用 t.Abort() 会出现异常的 但是我的while死循环是放在一个thread中的,结束按钮是放在主线程中的,我觉得两个线程是独立运行的,为什么控制图形界面的主线程也卡住了呢? 您好,请问能不能把您的socket开始监听部分代码发一份呢? 楼主,你根本没理解finally的意义,你在那里面搞了个侦听,回去好好看书吧。这么做当然要死机了。dispose()被你用死循环限制了,释放不掉了。 '停止监听线程 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 在找问题之前,建议先把代码整理下把socket初始化、bind及listen的操作放在线程外。把accept的处理放在线程中去,线程中止,除非没有办法,否则别用Abort,目前的操作,点abort,肯定抛异常出来,界面肯定也会卡要让线程自己执行完毕自然推出。 事务级别形同虚设? C#中如何用哈希表跟table中某列进行对比取不同的数据 未将对象引用设置到对象的实例。 无主键 数据库更新问题 如何打包并在其它机子上运行 在DOS控制台下用CSC命令来编译CS文件怎么不行啊 如何取Excel表中的身份证后六位作为用户密码 關於字符串查找的問題 谁有 物业管理软件 和房地产软件 本公司可以和你合作(西北地区)公司个人都可以,要求产品在一年以上。至少有2家典型客户。 c#如何获得字体文件的文件名及显示其样式 关于计算加班调休时间的问题 获取不到dropdownlist 的值
{
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();
}
} }
'停止监听线程
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
把socket初始化、bind及listen的操作放在线程外。
把accept的处理放在线程中去,
线程中止,除非没有办法,否则别用Abort,
目前的操作,点abort,肯定抛异常出来,
界面肯定也会卡
要让线程自己执行完毕自然推出。