private void Manage() { //1、找状态位没有被修改过的, //2、判断数据上传状态是否为中断,是即更新当前时间,否向下执行3,4,5 //3、监测网络状态,设定网络状态 //4、设定数据上传状态为中断 //5、向数据库中保存 //6、更新实时数据 while (true) { using (Lock l = new Lock(ulsList, 3000)) { for(int i= ulsList.Count-1;i>=0;i--) { try { MessageState.Label = ulsList[i].CoalCode + "," + ulsList[i].CoalName + "," + ulsList[i].CoalIP + "," + ulsList[i].UpLoadState + "," + ulsList[i].NetState + "," + ulsList[i].RealTime + "," + ulsList[i].StartTime+","+ulsList[i].ID; mcoper.ClientSendMessage(MessageState);//send state to web } catch (System.Transactions.TransactionAbortedException exec) { if (InitConfig.Debug) { Tcs.Libary.Log.Write(exec); } } catch (Exception exec) { if (InitConfig.Debug) { Tcs.Libary.Log.Write(exec); } } if (ulsList[i].Mark > 0) ulsList[i].RealTime = DateTime.Parse(ulsList[i].RealTime).AddSeconds(InitConfig.UPLF).ToString(); if (ulsList[i].Mark > 1 && !ulsList[i].IsSave) { string sql_Update = @"UPDATE [TN_ULAnalys] SET [RealTime] ='" + ulsList[i].RealTime + "' ,[Duration] = '" + DateTime.Parse(ulsList[i].RealTime).Subtract(DateTime.Parse(ulsList[i].StartTime)).TotalSeconds + "' WHERE [CoalCode] = '" + ulsList[i].CoalCode + "' AND [StartTime] = '" + ulsList[i].StartTime + "';"; if (!ulsList[i].NetProbe) { if (Ping.CmdPing(ulsList[i].CoalIP)) { ulsList[i].NetState = ULState.连接; ulsList[i].Result = "客户端可能已经停止运行或者客户端主机死机。"; } else { ulsList[i].NetState = ULState.中断; ulsList[i].Result = "客户端到服务端网络不通。"; } ulsList[i].UpLoadState = ULState.中断; } else { ulsList[i].NetState = ULState.连接; ulsList[i].UpLoadState = ULState.中断; ulsList[i].Result = "客户端数据无实时交换数据!"; } ulsList[i].IsSave = true; ulsList[i].StartTime = ulsList[i].RealTime; //中断后更新连接的结束时间和持续时间并保存断网的相关信息但无持续时间 string sql_Insert = INSERT_ULS + "'" + ulsList[i].CoalCode + "','" + ulsList[i].CoalIP + "','" + ulsList[i].NetState + "','" + ulsList[i].UpLoadState + "','" + ulsList[i].RealTime + "','" + ulsList[i].StartTime + "','0','" + ulsList[i].Result + "')"; try { int result = dataAcc.ExecuteCommand(sql_Update + sql_Insert); } catch (System.Data.Common.DbException dbExe) { Tcs.Libary.Log.Write(dbExe); } } else if (ulsList[i].UpLoadState == ULState.中断) { bool res = Ping.CmdPing(ulsList[i].CoalIP); if (!res && ulsList[i].NetState == ULState.连接) { string sql_Update = @"UPDATE [TN_ULAnalys] SET [RealTime] ='" + ulsList[i].RealTime + "' ,[Duration] = '" + DateTime.Parse(ulsList[i].RealTime).Subtract(DateTime.Parse(ulsList[i].StartTime)).TotalSeconds + "' WHERE [CoalCode] = '" + ulsList[i].CoalCode + "' AND [StartTime] = '" + ulsList[i].StartTime + "';"; ulsList[i].NetState = ULState.中断; ulsList[i].Result = "客户端到服务端网络不通。"; ulsList[i].UpLoadState = ULState.中断; ulsList[i].StartTime = ulsList[i].RealTime; string sql_Insert = INSERT_ULS + "'" + ulsList[i].CoalCode + "','" + ulsList[i].CoalIP + "','" + ulsList[i].NetState + "','" + ulsList[i].UpLoadState + "','" + ulsList[i].RealTime + "','" + ulsList[i].StartTime + "','0','" + ulsList[i].Result + "')"; try { int result = dataAcc.ExecuteCommand(sql_Update + sql_Insert); } catch (System.Data.Common.DbException dbExe) { Tcs.Libary.Log.Write(dbExe); } } else if (res && ulsList[i].NetState == ULState.中断) { string sql_Update = @"UPDATE [TN_ULAnalys] SET [RealTime] ='" + ulsList[i].RealTime + "' ,[Duration] = '" + DateTime.Parse(ulsList[i].RealTime).Subtract(DateTime.Parse(ulsList[i].StartTime)).TotalSeconds + "' WHERE [CoalCode] = '" + ulsList[i].CoalCode + "' AND [StartTime] = '" + ulsList[i].StartTime + "';"; ulsList[i].NetState = ULState.连接; ulsList[i].Result = "。"; ulsList[i].UpLoadState = ULState.中断; ulsList[i].StartTime = ulsList[i].RealTime; string sql_Insert = INSERT_ULS + "'" + ulsList[i].CoalCode + "','" + ulsList[i].CoalIP + "','" + ulsList[i].NetState + "','" + ulsList[i].UpLoadState + "','" + ulsList[i].RealTime + "','" + ulsList[i].StartTime + "','0','" + ulsList[i].Result + "')"; try { int result = dataAcc.ExecuteCommand(sql_Update + sql_Insert); } catch (System.Data.Common.DbException dbExe) { Tcs.Libary.Log.Write(dbExe); } } } ulsList[i].Mark++; ulsList[i].NetProbe = false; NetStateChangeEvent(int.Parse(ulsList[i].ID)); } // if (NetStateChangeEvent != null && ulsList.Count > 0) // NetStateChangeEvent(); } System.Threading.Thread.Sleep(InitConfig.UPLF*1000); } }
/// <summary> /// 更新网络状态 /// </summary> /// <param name="itemKey"></param> private void ULS_NetStateChange(int itemKey) { try { using (Lock l = new Lock(syncSave, 3000, false)) { /*--------------------------------------------由删除后从新添加更改为找到SubItem然后更新内容---------------------------------*/ int items = lvRealNetState.Items.Count; ULSModel ulsModel = UpLoadState.ulsList.Find(u => u.ID == itemKey.ToString()); bool isChange = false; for (int i = 0; i < items; i++) { if (int.Parse(lvRealNetState.Items[i].SubItems[0].Text) == itemKey) { lvRealNetState.Items[i].SubItems[1].Text = ulsModel.CoalCode; lvRealNetState.Items[i].SubItems[2].Text = ulsModel.CoalName; lvRealNetState.Items[i].SubItems[3].Text = ulsModel.UpLoadState.ToString(); lvRealNetState.Items[i].SubItems[4].Text = ulsModel.NetState.ToString(); lvRealNetState.Items[i].SubItems[5].Text = ulsModel.CoalIP; //lvRealNetState.Items[i].SubItems[6].Text = (DateTime.Parse(ulsModel.RealTime).Subtract(DateTime.Parse(ulsModel.StartTime)).TotalSeconds.ToString() + "(秒)"); lvRealNetState.Items[i].SubItems[6].Text = TimeSp(DateTime.Parse(ulsModel.StartTime), DateTime.Parse(ulsModel.RealTime)); isChange = true; break; } } if (!isChange) AddNewItem(ulsModel); } } catch (System.OverflowException ex) { Tcs.Libary.Log.Write("ULS_NetStateChange方法错误:" + ex.ToString()); } catch (System.ArgumentOutOfRangeException ae) { Tcs.Libary.Log.Write("ULS_NetStateChange方法错误:" + ae.ToString()); } catch (System.Exception ex) { Tcs.Libary.Log.Write("ULS_NetStateChange方法错误:" + ex.ToString()); } }
private void ReciveState() { System.Messaging.Message Message = null; System.Threading.Thread.Sleep(5000); while (true) { try { Message = null; Message = InitConfig.MessQueueState.Receive(new TimeSpan(0, 0, 1)); } catch (TimeoutException) { } catch (System.Messaging.MessageQueueException) { } catch (Exception) { } if (Message != null) { using (Lock l = new Lock(lockAsy, 500, false)) { UpdateReciveState(Message.Label.Split(',')); } } System.Threading.Thread.Sleep(1); } }