/// <summary> /// 进度条更改时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProgBar_backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (ProgPauseflag == 0) { //进度条执行到80%时 if (Convert.ToInt32(FtoR_progressBarControl.EditValue.ToString()) > (0.8 * FtoR_progressBarControl.Properties.Maximum)) { FtoR_progressBarControl.Properties.Step = 5000; } //进度条执行到90%时 if (Convert.ToInt32(FtoR_progressBarControl.EditValue.ToString()) > (0.9 * FtoR_progressBarControl.Properties.Maximum)) { completingflag = 1; ProgPauseflag = 1; } FtoR_progressBarControl.PerformStep(); } else //当前为暂停状态时,ProgPauseflag值为1 { changestep = changestep + 5; if (changestep * 10000 > changelength && completingflag == 0) { ProgPauseflag = 0;//完成循环,取消暂停状态 changestep = 0; changelength = 0; } } FtoR_progressBarControl.Update(); }
/// <summary> /// 文件监测 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FilesOnChanged(object source, FileSystemEventArgs e) { //当文件创建或更改时执行此方法 if (e.ChangeType == WatcherChangeTypes.Changed || e.ChangeType == WatcherChangeTypes.Created) { FileInfo prefile = new FileInfo(e.FullPath); FileInfo file = null; if (prefile.Extension == "") { file = new FileInfo(e.FullPath + "\\w001001.adf"); } else { file = new FileInfo(e.FullPath); } //检查是否跨线程调用 if (FtoR_progressBarControl.InvokeRequired) { //将方法委托到主线程 methoddelegate mydelegate = new methoddelegate((object source1, FileSystemEventArgs e1) => FilesOnChanged(source, e)); FtoR_progressBarControl.Invoke(mydelegate, source, e); } else { FileSystemWatcher filewatcher = source as FileSystemWatcher; if (Convert.ToInt32(FtoR_progressBarControl.EditValue.ToString()) == FtoR_progressBarControl.Properties.Maximum) { //根据进度条当前值判断转换结束 filewatcher.EnableRaisingEvents = false; filewatcher.Changed -= new FileSystemEventHandler(FilesOnChanged); filewatcher.Created -= new FileSystemEventHandler(FilesOnChanged); return; } //停止计时,估算结果文件大小并重新设置进度条属性 watch.Stop(); if (FtoR_progressBarControl.Properties.Maximum == 100000) { int maximum = (int)watch.ElapsedMilliseconds * 2910; FtoR_progressBarControl.Properties.Maximum = maximum; FtoR_progressBarControl.Properties.Step = 10000; } int value = FtoR_progressBarControl.Properties.Maximum / 1000; if (file.Exists) { if (Convert.ToInt32(FtoR_progressBarControl.EditValue.ToString()) <= (int)file.Length) { ProgPauseflag = 0;//文件长度大于进度条当前值 FtoR_progressBarControl.EditValue = file.Length; } else { ProgPauseflag = 1;//文件长度小于进度条当前值,暂停并等待 changelength = Convert.ToInt32(FtoR_progressBarControl.EditValue.ToString()) - (int)file.Length; } } } } }