progressBarControl 和 backgroundWorker 同时使用方法
private void BtnGpsExportAll_Click(object sender, EventArgs e) { if (backgroundWorker1.IsBusy) { MessageDxUtil.ShowTips("正在执行轨迹导出,请稍后在尝试!"); return; } if (this.winGridViewPager1.DataSource is List<QueryScoreInfo> entitys) { if (MessageDxUtil.ShowYesNoAndTips($"当前页可导出数据共计:{entitys.Count}条,确认导出?") == DialogResult.Yes) { // 后台线程查询数据 progressBarControl1.Position = 0; this.backgroundWorker1.RunWorkerAsync(entitys); // 读取数据的时候禁用,否则读取数据的时候会调用到最新的日期 SetControlEnabled(false); conn.WriteDBLog(null, null, "成绩查询", "页面轨迹导出", "成功", LoginUserInfo, 0, ""); } } } #endregion #region 取消导出 private void Btn_Cancel_Click(object sender, EventArgs e) { if (backgroundWorker1.IsBusy) backgroundWorker1.CancelAsync(); } #endregion
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { if (e.Argument is List<QueryScoreInfo> entitys) { string dirSt = this.dateEdit_St.DateTime.ToString("yyyyMMdd"); string dirEd = this.dateEdit_Ed.DateTime.ToString("yyyyMMdd"); int index = 1; int count = entitys.Count; foreach (var entity in entitys) { #region 生成轨迹文件 string startrq = entity.KSSJ1.Replace(":", ""); string endrq = entity.JSSJ1.Replace(":", ""); string ksfs = entity.JGFS1.ToString(); if (entity.JSSJ2 != null && entity.JSSJ2.Trim().Length > 0) { endrq = entity.JSSJ2.Replace(":", ""); ksfs = entity.JGFS2.ToString(); } //考生身份证-考试流水号-考试车辆编号-考试分数 string carno = entity.KCBH; string sfzhm = entity.ZJHM; string lsh = entity.LSH; startrq = entity.KSRQ1.Replace("-", "") + startrq; endrq = entity.KSRQ1.Replace("-", "") + endrq; DataTable dtgps = conn.GetGpsContent(startrq, endrq, carno, sfzhm); //string SaveGpsPath = "D:\\国华视频"; string SaveGpsPath = Path.Combine($"D:\\国华视频", $"{dirSt}-{dirEd}"); if (!Directory.Exists(SaveGpsPath)) Directory.CreateDirectory(SaveGpsPath); SaveGpsPath = Path.Combine(SaveGpsPath, $"{sfzhm}_{lsh}_{carno}_{ksfs}.txt"); using (FileStream fs = new FileStream(SaveGpsPath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { foreach (DataRow dr in dtgps.Rows) { string gps1 = ""; if (dr["GPSCON1"].ToString().IndexOf("KSXT") > -1) { gps1 = dr["GPSCON1"].ToString(); } string gps2 = ""; if (dr["GPSCON2"].ToString().IndexOf("KSXT") > -1) { gps2 = dr["GPSCON2"].ToString(); } sw.WriteLine(gps1 + gps2 + "\n"); } } } #endregion backgroundWorker1.ReportProgress(index * 100 / count); if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } index++; } } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBarControl1.Position = e.ProgressPercentage; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBarControl1.Position = 100; SetControlEnabled(true); MessageDxUtil.ShowTips("当前页面下载完成"); }
private void SetControlEnabled(bool isEnabled) { btnSearch.Enabled = isEnabled; btnDetail.Enabled = isEnabled; btnPrint.Enabled = isEnabled; btnRePrint.Enabled = isEnabled; btnVideoPlay.Enabled = isEnabled; btnGpsExport.Enabled = isEnabled; }
本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/13814937.html