public void Insert(ModbusOperationDelegate operation) { ModbusOperationDelegate[] delegateArray = this.queue.ToArray(); this.queue.Clear(); this.queue.Enqueue(operation); foreach (ModbusOperationDelegate delegate2 in delegateArray) { this.queue.Enqueue(delegate2); } }
private void TryOperate(ModbusOperationDelegate operate) { try { lock (this.serialPort) { while (true) { if (!this.serialPort.IsOpen) { this.serialPort.Open(); operate(); this.serialPort.Close(); break; } CancellationToken token = this.cts.Token; if (token.IsCancellationRequested) { throw new Exception("Process end"); } Thread.Sleep((int)(200 + this.r.Next(200, 800))); } } } catch (Exception exception) { if (exception is UnauthorizedAccessException) { this.TryOperate(operate); } else if (exception is IOException) { this.model.SetOperationStatus(this.serialPort.PortName + "SerialPortError", true); //((StationDataModel)this.model).IsRealTime = false; } else { if (exception is TimeoutException) { this.queue.Clear(); } if (this.serialPort.IsOpen) { this.serialPort.Close(); } this.model.SetOperationStatus(exception.Message + "(" + this.serialPort.PortName + ")", true); //(StationDataModel)this.model).IsRealTime = false; } } }
public void Add(ModbusOperationDelegate operation) { this.queue.Enqueue(operation); }