public void RunTimer() { StateObjClass StateObj = new StateObjClass(); StateObj.TimerCanceled = false; StateObj.SomeValue = 1; System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerTask); System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, StateObj, 0, 3000); // Save a reference for Dispose. StateObj.TimerReference = TimerItem; CPacket msg = CPacket.create(); JsonObjectCollection collection = new JsonObjectCollection(); collection.Add(new JsonStringValue("PROTOCOL_ID", "PING")); msg.push(collection.ToString()); this.send(msg); if (SocketExtensions.IsConnected(this.socket)) { Console.WriteLine("true"); } else { StateObj.TimerReference.Change(Timeout.Infinite, Timeout.Infinite); //Change Console.WriteLine("false(("); } }
public void start_keepalive() { System.Threading.Timer keepalive = null; keepalive = new System.Threading.Timer((object e) => { CPacket msg = CPacket.create(); JsonObjectCollection collection = new JsonObjectCollection(); collection.Add(new JsonStringValue("PROTOCOL_ID", "PING")); msg.push(collection.ToString()); this.send(msg); if (SocketExtensions.IsConnected(this.socket)) { Console.WriteLine("true"); } else { keepalive.Change(Timeout.Infinite, Timeout.Infinite); Console.WriteLine("false"); } }, null, 0, 3000); }