Abort a Thread

hello, i have a problem that i really dont understand !!,, i cant abort a thread, my app get frozen... i have this function:

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


Answer this question

Abort a Thread

  • RayinSpain

    No, you must re-initialize/re-create a Thread object.


  • Arindam Sen

    its me again,, now i cant start the thread again :P ... is there any way to abort it and be able to start it again later

    mig16

  • JSensei

    omg!! cant beleive this... with another thread is happening me the same thing! and its not even related with my tcplistener ,,, its just a thread that i cant stop,, my 2 threads cat be stoped... but WTF!!!...they worked in the last app i did.. and im using almost the same code,,,

    is there any other way of using threads maybe another class for thread :(

    mig16

  • Hibri Marzook

    thanks man,,, i dont know why buut the thread still dont close... it threadstate after the while() is abort reqeusted,,,,

    any more ideas =(

    mig16

  • Nimrand

    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.



  • PeteJM01

    How do you start the thread and do you store the thread object in a roaming variable


  • Dortoh

    You must first stop the TcpListener, here is some code that should do the trick:


    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();
    }
    }




  • JBond007

    It keeps running the while over and over Are you sure you Stop the TcpListener

    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.


  • Rob O

    mmmmmmmm im an $#%#"$%$#"%°!!!!!

    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 :)

  • Abort a Thread