Problem on Threading and TcpListen

  • Thread starter Thread starter Cylix
  • Start date Start date
C

Cylix

I have a tcpListen on a new thread in my application.
When start the tcpListen, the process will hold in that line.
I would like to terminate the tcpListen by abort the thread, but fail.

How can I abost the tcpListen in a indepent thread?

Thanks a lot!
 
Cylix said:
I have a tcpListen on a new thread in my application.
When start the tcpListen, the process will hold in that line.
I would like to terminate the tcpListen by abort the thread, but fail.

How can I abost the tcpListen in a indepent thread?

Thanks a lot

The reason you can't use Thread.Abort is that Thread.Abort is a request
to the thread to shutdown at the earlieast possible moment. If that
thread happens to be in Unmanaged code (as it is, while the socket is
in a listening state - ie a call to accept), it will not recieve that
request (a threadabortexception) until it returns from that call. So,
unless a client happens to connect you won't abort.

So, the common way to overcome that is to make sure you have a
reference to the actual listener, and then call close on it. This will
cause it to leave the listen state and then you can shutdown the
thread.
 
I have some code that can point you in the right direction. Email me and
I'll send it to you...
 
Back
Top