多线程退出问题
在VS2005下开发了一个客户端与服务端通信的程序!
我在客户端程序中使用了多线程!
可是在客户端程序关闭后,仍占用系统部分系统资源!
只有在服务端程序进程终止之后,客户端资源才可以得到释放!
请问是什么原因?
客户端程序代码:
private IPAddress myIP = IPAddress.Parse( "192.168.0.1 ");
private IPEndPoint MyServer;
private Socket sock;
private bool bb = true;
public string recvStr = " "; //接收到字符串
public Thread thread;
private void btnCon_Click(object sender, EventArgs e)
{
MyServer = new IPEndPoint(myIP, Int32.Parse( "8888 "));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(MyServer);
this.statusBar1.Text = "与主机连接成功! ";
thread = new Thread(new ThreadStart(targett));
// thread.IsBackground = true;
thread.Start();
}
public delegate void MyOkInVoke(string str);
private void targett()
{
while (bb)
{
Byte[] bbb = new Byte[1024];
string aa = " ";
int bytes;
bytes = sock.Receive(bbb, bbb.Length, 0);
aa += System.Text.Encoding.Default.GetString(bbb, 0, bytes);
recvStr = recvStr+aa;
//传递参数
MyOkInVoke okCon = new MyOkInVoke(CreateNewForm);
this.BeginInvoke(okCon, new object[] { recvStr });
}
}
public void CreateNewForm(string str)
{
this.txtRecv.Text = recvStr; //刷新接收数据文本框
this.txtRecv.Update();
}
private void btnSend_Click(object sender, EventArgs e)//发送数据
{
Byte[] bytee = new Byte[1024];
string send = this.txtSend.Text.ToString();
bytee = System.Text.Encoding.Default.GetBytes(send.ToCharArray());
sock.Send(bytee, bytee.Length, 0);
}
private void btnClose_Click(object sender, EventArgs e)//关闭连接中断线程
{
try
{
sock.Close();
thread.Abort();
this.statusBar1.Text = "与服务器断开连接! ";