public void VisibleInstance() { this.ComponentThread = new Thread(new ThreadStart(() => this.VisibleInstanceRoutine.Get())); // set the apartment state -- COM contoles run under Single Thread Apartment ComponentThread.SetApartmentState(ApartmentState.STA); ComponentThread.IsBackground = true; }
protected override void AfterMyWorkStateChanged(object sender, EventArgs e) { if (myWorkState) { var ElectricConfig = MysqlMethod.Search_ElectricConfig(GatewayConfig.GatewayIndex); foreach (var item in ElectricConfig) { ElectricTypeEnum electricTypeEnum = (ElectricTypeEnum)item.ElectricTypeEnum; switch (electricTypeEnum) { case ElectricTypeEnum.BAW_4C: { BAW_4CProtocol protocol = new BAW_4CProtocol() { ID = (byte)item.DeviceID, GatewayIndex = item.GatewayIndex, DeviceIndex = item.DeviceIndex, ElectricTypeEnum = item.ElectricTypeEnum, LoopTypeEnum = item.LoopTypeEnum, PhaseTypeEnum = item.PhaseTypeEnum, PhaseAngleTypeEnum = item.PhaseAngleTypeEnum, MysqlMethod = MysqlMethod }; ElectricAbsProtocols.Add(protocol); } break; } } ComponentThread = new Thread(Analysis); ComponentThread.Start(); } else { if (ComponentThread != null) { ComponentThread.Abort(); } } }
protected override void AfterMyWorkStateChanged(object sender, EventArgs e) { if (myWorkState) { int Baudate = Convert.ToInt32(GatewayConfig.Rate.Split(',')[0]); int DataBits = Convert.ToInt32(GatewayConfig.Rate.Split(',')[1]); string parity = GatewayConfig.Rate.Split(',')[2]; int stopbits = Convert.ToInt32(GatewayConfig.Rate.Split(',')[3]); RS485 = new SerialPort(GatewayConfig.Location); RS485.BaudRate = Baudate; RS485.DataBits = DataBits; switch (parity) { case "N": { RS485.Parity = Parity.None; } break; case "O": { RS485.Parity = Parity.Odd; } break; case "E": { RS485.Parity = Parity.Even; } break; } RS485.StopBits = (StopBits)stopbits; var ElectricConfig = MysqlMethod.Search_ElectricConfig(GatewayConfig.GatewayIndex); foreach (var item in ElectricConfig) { ElectricTypeEnum electricTypeEnum = (ElectricTypeEnum)item.ElectricTypeEnum; switch (electricTypeEnum) { case ElectricTypeEnum.BAW_4C: { BAW_4CProtocol protocol = new BAW_4CProtocol() { ID = (byte)item.DeviceID, GatewayIndex = item.GatewayIndex, DeviceIndex = item.DeviceIndex, ElectricTypeEnum = item.ElectricTypeEnum, LoopTypeEnum = item.LoopTypeEnum, PhaseTypeEnum = item.PhaseTypeEnum, PhaseAngleTypeEnum = item.PhaseAngleTypeEnum, MysqlMethod = MysqlMethod }; ElectricAbsProtocols.Add(protocol); } break; } } ComponentThread = new Thread(Analysis); ComponentThread.Start(); } else { if (RS485.IsOpen) { RS485.Close(); } if (ComponentThread != null) { ComponentThread.Abort(); } } }