private void button12_Click(object sender, EventArgs e) { if (null != _perRaido) { _perRaido.WaitToStop(); _perRaido = null; } else { _perRaido = new Hik.Threading.Timer(100); _perRaido.Elapsed += __PerRadio_Elapsed; _perRaido.Start(); } }
/// <summary> /// Starts the thread to receive messages from socket. /// </summary> protected override void StartInternal() { _running = true; IAsyncResult res = _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), null); #if UTILIZA_DESCONEXION_AUTOMATICA // if (res.IsCompleted) { timerTimeout = new Threading.Timer(120000); timerTimeout.Elapsed += new EventHandler(timerTimeout_Elapsed); timerTimeout.Start(); } #endif }
/// <summary> /// This method is used as callback method in _clientSocket's BeginReceive method. /// It reveives bytes from socker. /// </summary> /// <param name="ar">Asyncronous call result</param> private void ReceiveCallback(IAsyncResult ar) { if (!_running) { return; } #if UTILIZA_DESCONEXION_AUTOMATICA //int valorAnterior = Interlocked.CompareExchange(ref timeoutFlag, 2, 1); if (Interlocked.CompareExchange(ref timeoutFlag, 2, 1) /*valorAnterior*/ != 0) { //El flag ya ha sido seteado con lo cual nada!! return; } if (timerTimeout != null) { timerTimeout.Stop(); } #endif try { //Get received bytes count var bytesRead = _clientSocket.EndReceive(ar); if (bytesRead > 0) { LastReceivedMessageTime = DateTime.Now; //Copy received bytes to a new byte array var receivedBytes = new byte[bytesRead]; Array.Copy(_buffer, 0, receivedBytes, 0, bytesRead); //Read messages according to current wire protocol var messages = WireProtocol.CreateMessages(receivedBytes); //Raise MessageReceived event for all received messages foreach (var message in messages) { OnMessageReceived(message); } } else { throw new CommunicationException("Tcp socket is closed"); } //Read more bytes if still running if (_running) { _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), null); #if UTILIZA_DESCONEXION_AUTOMATICA timerTimeout.Start(); #endif } } catch { Disconnect(); } }