public static bool RunTaskWithTimeoutDT <DataTableT>(Func <DataTable> TaskAction, out DataTable dtR, int TimeoutMillisecond, MesSession _session, TransData _transData, string actionParam, string caller, bool toPlc = true) { LogInfo log; Task <DataTable> backgroundTask; dtR = null; try { backgroundTask = Task.Factory.StartNew(TaskAction); backgroundTask.Wait(new TimeSpan(0, 0, 0, 0, TimeoutMillisecond)); } catch (AggregateException ex) { var failMessage = ex.Flatten().InnerException.Message; log = new SocketService.LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口超时(>{TimeoutMillisecond}ms),原因:{failMessage},执行参数:{actionParam}."); _transData.ProcessData = $"Mission Timeout({TimeoutMillisecond}ms) , Please check the database connection"; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(false); } catch (Exception ex) { var failMessage = ex.Message; log = new SocketService.LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口超时(>{TimeoutMillisecond}ms),原因:{failMessage},执行参数:{actionParam}."); _transData.ProcessData = $"Mission Timeout({TimeoutMillisecond}ms) , Please check the database connection"; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(false); } if (!backgroundTask.IsCompleted) { log = new SocketService.LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口超时(>{TimeoutMillisecond}ms),请检查网络状况或数据库连接配置文件是否正确,执行参数:{actionParam}."); _transData.ProcessData = $"Mission Timeout({TimeoutMillisecond}ms) , Please check the database connection"; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(false); } // task succeeded dtR = backgroundTask.Result; // 判断API是否执行成功 --------------------------------- //string checkStatusR = DataTableHelper.GetCellValueinDT(dtR, 0, "CheckStatus"); //string checkMsgR = DataTableHelper.GetCellValueinDT(dtR, 0, "ReturnMsg"); string checkStatusR = DataTableHelper.GetCellValueinDT(dtR, 0, 0); string checkMsgR = DataTableHelper.GetCellValueinDT(dtR, 0, 1); if (checkStatusR == "-1") { log = new LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口失败>> 接口返回({checkStatusR}---{checkMsgR}),执行参数:{actionParam}。"); _transData.ProcessData = $"Error:{checkMsgR}"; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(false); } log = new LogInfo(_session, LogLevel.Info, $"[{_transData.SN}]执行【{caller}】接口成功>> 执行参数:{actionParam}。"); return(true); }
private void SetTreeNodeStatus(TransData transData) { this.Invoke(new ThreadStart(delegate { tvProtocol.BeginUpdate(); //设置工单 if (transData.FuncCode == "WOR") { lblOrder.Text = transData.ProcessData; } //设置SN if (transData.FuncCode == "PTR") { lblSN.Text = transData.SN; lstProtocol.ForEach(li => { if (li.ParentID != "null") { string[] arrs = li.Content.Split(';'); arrs[3] = transData.SN; li.Content = StringHelper.GetArrayStr(arrs.ToList(), ";"); } }); } Protocol item = lstProtocol.FindLast(li => li.IsSend == true && li.ParentID != "null"); if (item != null) { TreeNode node = tvProtocol.Nodes.Find($"{item.ParentID}_{item.ID}", true).FirstOrDefault(); node.Text = $"{item.Name} ---> [{item.Content}] <{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>"; if (transData.Status == "OK") { node.ImageIndex = 3; node.SelectedImageIndex = 3; node.ForeColor = Color.DarkCyan; } else { node.ImageIndex = 2; node.SelectedImageIndex = 2; node.ForeColor = Color.Crimson; } node.EnsureVisible();// 滚动条 bool finish = true; foreach (TreeNode tNode in node.Parent.Nodes) { if (tNode.ImageIndex != 3)//存在没完成的项目 { finish = false; } } TreeNode pNode = tvProtocol.Nodes.Find($"{item.ParentID}", true).FirstOrDefault(); if (finish) { pNode.ImageIndex = 3; pNode.SelectedImageIndex = 3; } else { pNode.ImageIndex = 1; pNode.SelectedImageIndex = 1; } tvProtocol.Refresh(); } tvProtocol.EndUpdate(); })); }
public static T RunTaskWithTimeout <T>(Func <T> TaskAction, int TimeoutMillisecond, MesSession _session, TransData _transData, string actionParam, string caller, bool toPlc = true) { LogInfo log; Task <T> backgroundTask; try { backgroundTask = Task.Factory.StartNew(TaskAction); backgroundTask.Wait(new TimeSpan(0, 0, 0, 0, TimeoutMillisecond)); } catch (AggregateException ex) { // task failed var failMessage = ex.Flatten().InnerException.Message; log = new SocketService.LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口超时(>{GlobalData.ApiTimeout}ms),原因:{failMessage},执行参数:{actionParam}."); _transData.ProcessData = $"Mission Timeout({TimeoutMillisecond}ms) , Please check the database connection"; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(default(T)); } catch (Exception ex) { // task failed var failMessage = ex.Message; log = new SocketService.LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口超时(>{GlobalData.ApiTimeout}ms),原因:{failMessage},执行参数:{actionParam}."); _transData.ProcessData = $"Mission Timeout({TimeoutMillisecond}ms) , Please check the database connection"; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(default(T)); } if (!backgroundTask.IsCompleted) { // task timed out log = new SocketService.LogInfo(_session, LogLevel.Error, $"[{_transData.SN}]执行【{caller}】接口超时(>{GlobalData.ApiTimeout}ms),请检查网络状况或数据库连接配置文件是否正确,执行参数:{actionParam}."); _transData.ProcessData = $"Mission Timeout({TimeoutMillisecond}ms) , Please check the database connection,Check SQLServer Connection. "; if (toPlc) { EmployeeComm.SendMsg(_session, _transData, CheckResult.NG); } return(default(T)); } // task succeeded log = new LogInfo(_session, LogLevel.Info, $"[{_transData.SN}]执行【{caller}】接口成功>> 执行参数:{actionParam}。"); return(backgroundTask.Result); }