public void AsyncRead(cbFncBytesToRead read, bool start) { if (start) { //Connect(LEDDetect.Properties.Settings.Default.IP_ADDRESS, LEDDetect.Properties.Settings.Default.PORT); readbytes = new Thread(() => Read(read)); readbytes.Start(); threadRun = true; } else { if (readbytes != null)//&& (readbytes.ThreadState == ThreadState.Running || readbytes.IsAlive == true)) { threadRun = false; Thread.Sleep(1000); readbytes = new Thread(() => Read(read)); readbytes = null; //Connect(LEDDetect.Properties.Settings.Default.IP_ADDRESS, LEDDetect.Properties.Settings.Default.PORT); } } }
private byte[] Read(cbFncBytesToRead read) { string msgtxt; msgtxt = ""; msgcount = 0; while (threadRun) { readlen = 0; if (tcpclnt.Connected) { try { byte[] bb = new byte[5000000]; // stm.ReadTimeout = 1000; if (stm.DataAvailable) { // long tmp = stm.Length; int k = stm.Read(bb, 0, 5000000); // stm.Flush(); msgcount = msgcount + k; if (k > 0) { Array.Resize(ref bb, k); read(bb); } } } catch (System.IO.IOException ex) { msgtxt = "stm: " + stm.CanRead.ToString() + " tcp: " + tcpclnt.Connected.ToString(); MessageBox.Show(ex.Message, msgtxt); //Environment.Exit(Environment.ExitCode); // break; } } // if(tcpclnt.Connected) } return(null); }