我在一个工作线程中用NetworkStream定义stream.Read读取TcpClient接收到的数据,这个时候线程会停在这里不往下执行,所以我用stream.ReadTimeout = 1000;让他超时,这种方法在.net环境中可以正常退出,但是单独执行程序却不能退出,请高手指点!谢谢!
public void receive()
{
while (!bExit)
{
Byte[] data = new Byte[8192];
try
{
stream.ReadTimeout = 1000;
Int32 bytes = stream.Read(data, 0, data.Length);
// String to store the response ASCII representation.
String responseData = String.Empty;
responseData = System.Text.Encoding.Default.GetString(data, 0, bytes);
this.Invoke(myRData, responseData);
}
catch (IOException e1)
{
}
}
}private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bExit = true;
tcpRecv.Join();
try
{
stream.Close();
client.Close();
}
catch (Exception ee1)
{
MessageBox.Show(ee1.Message);
}
}
public void receive()
{
while (!bExit)
{
Byte[] data = new Byte[8192];
try
{
stream.ReadTimeout = 1000;
Int32 bytes = stream.Read(data, 0, data.Length);
// String to store the response ASCII representation.
String responseData = String.Empty;
responseData = System.Text.Encoding.Default.GetString(data, 0, bytes);
this.Invoke(myRData, responseData);
}
catch (IOException e1)
{
}
}
}private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bExit = true;
tcpRecv.Join();
try
{
stream.Close();
client.Close();
}
catch (Exception ee1)
{
MessageBox.Show(ee1.Message);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货