hola
Estoy haciendo un programa que habre un formulario, y he creado un
thread, para que este escuchando un puerto en concreto. Me funciona bien,
pero cuando cierro el formulario, el ejecutable sige, voy al administrador
de tareas y sigue ahi. Yo creo que el error esta en que queda algun hilo
abierto, y por eso no se cierra ¿Como podria matar ese hilo?
El codigo es el siguiente:
private void app_Load(object sender, System.EventArgs e)
{
Thread thread=new Thread(new ThreadStart(recibirSocket));
thread.Start();
}
private void recibirSocket()
{
myServer=new IPEndPoint(myIP,Int32.Parse("20000"));
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socket.Bind(myServer);
socket.Listen(50);
while(true)
{
try
{
accSocket=socket.Accept();
if(accSocket.Connected)
{
Thread thread=new Thread(new ThreadStart(leerDatos));
thread.Start();
}
private void leerDatos()
{
Byte[]rec=new Byte[1024];
NetworkStream acceptStream=new NetworkStream(accSocket);
int i=0;
while((i=acceptStream.Read(rec,0,rec.Length))!= 0)
{
string recMessage=System.Text.Encoding.Default.GetString(rec);
rec=new Byte[1024];
txtRecibido.Text=recMessage;
}
}
Leer las respuestas