public void WriteException(Exception ex) { // 在本地记录异常 LogTextHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); }
void worker_DoWork(object sender, DoWorkEventArgs e) { int itemCount = 0; int errorCount = 0; if (myDs != null && myDs.Tables[0].Rows.Count > 0) { //定义步长 double step = 50 * (1.0 / myDs.Tables[0].Rows.Count); DataTable dt = myDs.Tables[0]; int i = 1; try { foreach (DataRow dr in dt.Rows) { if (OnDataSave != null) { #region 保存操作,如果有错误,则记录并处理 try { bool success = OnDataSave(dr); if (success) { itemCount++; } } catch (Exception ex) { errorCount++; LogTextHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); } if (errorCount >= 3) { var format = "记录导入已经连续出错超过[{0}]条,您是否确定退出导入操作?\r\n单击【是】退出导入,单击【否】忽略错误,继续导入下一记录。"; format = JsonLanguage.Default.GetString(format); string message = string.Format(format, errorCount); if (MessageDxUtil.ShowYesNoAndWarning(message) == System.Windows.Forms.DialogResult.Yes) { break; } else { errorCount = 0;//置零重新计算 } } #endregion } int currentStep = Convert.ToInt32(step * i); worker.ReportProgress(currentStep); i++; } if (itemCount == dt.Rows.Count) { e.Result = "操作成功"; } else { e.Result = "操作完成,有错误可能未导入全部"; } } catch (Exception ex) { e.Result = ex.Message; LogTextHelper.Error(ex); MessageDxUtil.ShowError(ex.ToString()); } } else { e.Result = "请检查数据记录是否存在"; } }