c# - How to abort thread that use AcceptTcpClient() inside? -


accepttcpclient() prevents app exit after called thrd.abort().

how exit application when in listening?

you should able interrupt call accepttcpclient() closing tcplistener (this result in exception being thrown blocking accepttcpclient(). should not aborting thread, bad idea in few specific circumstances.

here's brief example:

class program {     static void main(string[] args)     {         var listener = new tcplistener(ipaddress.any, 12343);         var thread = new thread(() => asyncaccept(listener));         thread.start();         console.writeline("press enter stop...");         console.readline();         console.writeline("stopping listener...");         listener.stop();         thread.join();     }      private static void asyncaccept(tcplistener listener)     {         listener.start();         console.writeline("started listener");         try         {             while (true)             {                 using (var client = listener.accepttcpclient())                 {                     console.writeline("accepted client: {0}", client.client.remoteendpoint);                 }             }         }         catch(exception e)         {             console.writeline(e);         }         console.writeline("listener done");     } } 

the code above starts listener on separate thread, pressing enter on console window stop listener, wait listener thread complete, application exit normally, no thread aborts required!


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -