c# - Unable to close loop running in thread at application close. tcp/ip -


i'm working on 1 tcp client server application , running tcp server in loop when close application loop continuously runs , send data device. here scenario: tcp server runs , listen client(handheld device) once device connected long there no data receive device server needs send empty data continuously device.

code:

private tcpmainlistener tcplistener; private thread listenthread; private tcpclient client; public bool checkformisclose { get; set; } list<thread> listenthreads; public void starttcpserver() { this.tcplistener = new tcpmainlistener(ipaddress.any, convert.toint32(tcpport)); this.listenthread = new thread(new threadstart(listenforclients)); this.listenthread.start(); listenthreads.add(listenthread); }   private void listenforclients()         {             try             {                 this.tcplistener.start();             }             catch (exception ex)             {                 throw ex;             }              while (!checkformisclose)             {                 try                 {                     //blocks until client has connected server                     client = (tcpclient)this.tcplistener.accepttcpclient();                      //create thread handle communication                      //with connected client                     thread clientthread = new thread(new parameterizedthreadstart(handleclientcomm));                     clientthread.start(client);                 }                 catch                 {                     break;                 }             }         } 

client handle:

 private void handleclientcomm(object client)         {             tcpclient tcpclient = (tcpclient)client;              networkstream clientstream = tcpclient.getstream();              byte[] message = new byte[100];             int bytesread;              while (true)             {                 bytesread = 0;                  try                 {                     //blocks until client sends message                     bytesread = clientstream.read(message, 0, 40);                      #region write client until message receive                     int timeout = 0;                      //send empty byte device until bytesread != 0                                         {                         thread.sleep(50);                         timeout++;                         if (timeout == 10)                         {                             byte[] b = new byte[] { 0x00 };                             tcpclient.getstream().write(b, 0, b.length);                             timeout = 1;                         }                     } while (bytesread == 0);                      #endregion                      if (bytesread != 0)                     {                         //message has been received                         if (bitconverter.tostring(message).replace("-", "").startswith("ff"))                         {                             cardprocessing(message);                         }                     }                 }                 catch                 {                     //a socket error has occured                     break;                 }             }              tcpclient.close();         } 

now here form closing code: , not doing @ all

private void tcpmainserver_formclosing(object sender, formclosingeventargs e)         {             foreach (thread thread in listenthreads)             {                 checkformisclose = true;                 system.threading.tasks.task.factory.startnew(() =>                 {                     checkformisclose = true;                     tcplistener.stop();                     process.getcurrentprocess().close();                     gc.suppressfinalize(thread);                     thread.join();                     thread.interrupt();                 });             }         }          private void tcpmainserver_formclosed(object sender, formclosedeventargs e)         {             foreach (thread thread in listenthreads)             {                 checkformisclose = false;                 system.threading.tasks.task.factory.startnew(() =>                 {                     tcplistener.stop();                     process.getcurrentprocess().close();                     gc.suppressfinalize(thread);                     thread.join();                     thread.interrupt();                 });             }         } 


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -