public void doStuff() { try { thisthread = Thread.CurrentThread; thisthread.Name = "Keep alive thread"; if (!running) { return; } Thread.Sleep(10 * 1000); while (running) { try { sendKeepAlive(); } catch (Exception ex) { parent.addLogMessage("Error sending KeepAlive message", true); } finally { resetSleepTime(); } } } catch (ThreadInterruptedException ex) { String s = ex.ToString(); } }
public virtual void ReadData() { try { byte[] myReadBuffer; StringBuilder builder; String buffer = ""; String[] CQMessages; String CQCommand; String tmp; int bytes = 0; int messageNumber = 0; long pos = 0; do { builder = new StringBuilder(); myReadBuffer = new byte[1024]; bytes = stream.Read(myReadBuffer, 0, myReadBuffer.Length); // pos = stream.Position; builder.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, bytes)); tmp = buffer + builder.ToString(); CQMessages = tmp.Split('\n'); for (int i = 0; i < (CQMessages.Length - 1); i++) { CQCommand = CQMessages[i].Trim(); messageNumber++; processMessage(CQCommand); } buffer = CQMessages[CQMessages.Length - 1]; }while ((bytes > 0) && (running)); running = false; parent.addLogMessage(getName() + " connection has closed", false); } catch (System.IO.IOException ex) { running = false; parent.addLogMessage("An IO exception occoured in " + getName(), true); } catch (SocketException ex) { running = false; parent.addLogMessage("A socket exception occoured in " + getName(), true); } finally { this.BeginClose(); } }