苏飞论坛

标题: C#task中程序挂起等待中,如果要取消task,要怎么实现 [打印本页]

作者: 王永磊    时间: 2019-6-3 11:16
标题: C#task中程序挂起等待中,如果要取消task,要怎么实现
比如这名Socket clientsocket = Servertsocket.Accept();当服务器绑定ip,端口后等待客户端连接,如果此时我要结束任务要怎么实现???
Task taskaccept = new Task(() =>
{
while (true)
{
Socket clientsocket = Servertsocket.Accept();
IPEndPoint endPoint = clientsocket.RemoteEndPoint as IPEndPoint;
if (Form1.frm1.textBox2.InvokeRequired)
{
Form1.frm1.textBox2.Invoke(action = () =>
{
Form1.frm1.textBox2.Text += endPoint.Address + "[" + endPoint.Port + "]连接成功" + "\r\n";
});
}
else
{
Form1.frm1.textBox2.Text += endPoint.Address + "[" + endPoint.Port + "]连接成功" + "\r\n";
}
}
}, cts1.Token);
}



作者: 站长苏飞    时间: 2019-6-3 16:09
clientsocket =null




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4