socket is my Socket
tcpListeer is a TcpListener
listening is a bool
listen is a Thread
private void StartListening() { socket = null; tcpListener = null; tcpListener = new TcpListener(IPAddress.Any, port); try { tcpListener.Start(); listening = true; lblConnection.Text = "Esperando Conexion en puerto " + port; btnListen.Text = "Cancelar"; //Here i wait for a connection and this can take long socket = tcpListener.AcceptSocket(); listening = false; connected = true; lblConnection.Text = "Conexion Aceptada " + socket.RemoteEndPoint; btnListen.Text = "Desconectar"; } catch (Exception ex) { lblConnection.Text = "Error al escuchar en puerto " + txtPort.Text; MessageBox.Show(ex.ToString()); } } |
if my user want to canel the listening on the port i have a button that cancels the thread this way:
private void btnCancel_Click(object sender, EventArgs e) { listen.Abort(); } |
but my app never abort it,,,, i als tried this:
listen.Abort(); listen.Join(); |
but still not working
why cant i abort a thread

Abort a Thread
AluminumPork
sorry for making u loose ur time.... the example u posted worked fine,, but i didnt copied right
i was using
while (listen.Join(100))
{
listen.Abort();
}
and forgot the '!'
thanks man :)
JeffreyCollins
private void button1_Click(object sender, System.EventArgs e)
{
_listener.Stop();
_listenThread.Abort();
while( !_listenThread.Join( 100 ) )
{
Console.WriteLine( "Thread not aborted, trying again." );
_listenThread.Abort();
}
}
foleyp
is there any other way of using threads maybe another class for thread :(
mig16
pellarin
toolman
DaPoussin
I have write a small test with you situation and stopping the TcPListener did the trick. You can drop the TcpListener class and use the Socket class instead, you have much more control when you use that class.
Marcello Consolo
mig16
huckleberry
Thank you for the tip! This works great to get out of the synchronous function AcceptTcpClient().
Now I have a similar problem that is I get stuck with another synchronous function NetworkStream.Read(); I close the socket and NetworkStream associated with the instance of TcpClient and call Abort() from the main thread, but the thread gets stuck with the function Read() unless the other end of the socket is closed.
Any hint to resolve this problem will be greatly appreciated.
Navaron
any more ideas =(
mig16