我要做一个监听程序不断的进行监听代码大致如下我在关闭事件时也做了Abort()但为什么还是会有进程存在。我很郁闷请各位大虾帮帮忙小弟在此谢了 !
private Socket accSock;
Thread thread, thread1; private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
thread = new Thread(new ThreadStart(accp));
thread.Start();
this.button1.Enabled = false;
this.button3.Enabled = true;
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
} private void accp()
{
MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
textBox3.AppendText("主机" + textBox1.Text + "端口" + textBox2.Text + "开始监听......\r\n");
while (true)
{
accSock = sock.Accept();
if (accSock.Connected)
{
textBox3.AppendText("与客户建立连接");
thread1 = new Thread(new ThreadStart(round));
thread1.Start();
}
else
return;
} } private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
thread.Abort();
thread1.Abort();
Application.Exit();
}
catch { }
}
private Socket accSock;
Thread thread, thread1; private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
thread = new Thread(new ThreadStart(accp));
thread.Start();
this.button1.Enabled = false;
this.button3.Enabled = true;
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
} private void accp()
{
MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
textBox3.AppendText("主机" + textBox1.Text + "端口" + textBox2.Text + "开始监听......\r\n");
while (true)
{
accSock = sock.Accept();
if (accSock.Connected)
{
textBox3.AppendText("与客户建立连接");
thread1 = new Thread(new ThreadStart(round));
thread1.Start();
}
else
return;
} } private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
thread.Abort();
thread1.Abort();
Application.Exit();
}
catch { }
}
while (true)
{
accSock = sock.Accept();
if (accSock.Connected)
{
textBox3.AppendText("与客户建立连接");
thread1 = new Thread(new ThreadStart(round));
thread1.IsBackground=true;
thread1.Start();
}
else
return;
}