public static bool Connect() { if (_tron != null) { return(Connected); } if (!EnableTronWindow || !ReConnect.Timeout) { return(false); } try { lock (CheckState) { _tron = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut, PipeOptions.Asynchronous); _tron?.Connect(10); } } catch (Exception) { Disconnect(); } ReConnect.Start(TimeSpan.FromSeconds(30)); return(Connected); }
private static void UpdateState() { if (!CheckState.Timeout) { return; } if (!Connect()) { return; } try { lock (CheckState) { _tron.WriteByte((byte)TronCommand.CheckTrace); var rxLength = 8; var rxBuffer = new byte[8]; rxLength = _tron.Read(rxBuffer, 0, rxLength); if (rxLength >= 4) { _traceState = rxBuffer[0]; _traceState += (long)rxBuffer[1] << 8; _traceState += (long)rxBuffer[2] << 16; _traceState += (long)rxBuffer[3] << 24; } } } catch (Exception) { Disconnect(); } CheckState.Start(10000); }