private void Dowork(object obj) { while (true) { List <Assembling> items = new List <Assembling>(); try { using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { items = dbContext.Assemblings.ToList(); } } catch { } foreach (var item in items) { AssemblingDevice device = null; dict.TryGetValue(item.Id, out device); if (device != null) { if (item.Status != device.Status) { if (item.Status == 1) { device.CallButton.Display(new Display900UItem(), LightColor.Green, true);//交互灯开启采集 } else { device.CallButton.Clear(true); } device.Status = item.Status; } } } Thread.Sleep(1000); } }
public void Start() { this.Stop(); try { List <Assembling> items = new List <Assembling>(); using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { items = dbContext.Assemblings.ToList(); } foreach (var item in items) { XGate xgate = this.xGates .FirstOrDefault(xg => xg.IPEndPoint.Address.ToString() == item.XGateIP); if (xgate == null) { xgate = new XGate(item.XGateIP); this.xGates.Add(xgate); } RS485Bus bus = xgate.Buses[(byte)item.Bus]; Ptl900U callButton = (Ptl900U)bus.Devices.FirstOrDefault(d => d.Address == (byte)item.Address); if (callButton == null) { callButton = new Ptl900U(); callButton.Address = (byte)item.Address; bus.Devices.AddOrUpdate(callButton); } AssemblingDevice logicDevice = new AssemblingDevice { XGate = xgate, Bus = bus, CallButton = callButton, Id = item.Id, AreaId = item.AreaId, Type = item.Type }; this.devices.Add(logicDevice); } //启动 PTL 通讯 foreach (XGate xGate in this.xGates) { xGate.StartUnicastCommandQueue(); foreach (RS485Bus bus in xGate.Buses) { foreach (PtlDevice target in bus.Devices) { target.Initialize(); if (target is Ptl900U) { (target as Ptl900U).Pressed += CallButton_Pressed; } } } } dict = this.devices.ToDictionary(x => x.Id); thread = new Thread(new ParameterizedThreadStart(Dowork)); thread.IsBackground = true; thread.Start(); //threadStatus = new Thread(new ParameterizedThreadStart(DoStatus)); //threadStatus.IsBackground = true; //threadStatus.Start(); var groups = this.devices.GroupBy(x => x.AreaId); foreach (var group in groups) { //dictBool.AddOrUpdate(group.Key, false, (a, b) => b); Thread threadStatus = new Thread(new ParameterizedThreadStart(DoStatus)); threadStatus.IsBackground = true; threadStatus.Start(group.Key); dictThread.AddOrUpdate(group.Key, threadStatus, (a, b) => b); } } catch { this.Stop(); } }