我有一个这样的话题:
static void scan() {
while(true) {
client = server.AcceptTcpClient();
byte[] receivedBuffer = new byte[100];
NetworkStream stream = client.GetStream();
stream.Read(receivedBuffer, 0, receivedBuffer.Length);
string msg = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);
Form1 form = new Form1();
form.comboBox1.Items.Add(msg);
}
}
当我退出应用程序时,该线程仍在后台运行,并且thread.Abort()
不起作用,因为server.AcceptTcpClient()
仍在等待连接,并且永远都不会到达循环的结尾。如何设置此线程的最大执行时间,但仅在请求中止(例如使用布尔值)时才设置?