/// <summary> /// 同步文件后台任务执行完成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SyncFileTaskComplate(object sender, RunWorkerCompletedEventArgs e) { var rb = (bool)e.Result; OnTaskBarEvent?.Invoke(WindowUtil.CalcProgress(new MainWindowStatusNotify(), rb ? "同步完成" : "任务中止", 100)); }
/// <summary> /// 执行同步文件的后台任务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DoSyncFileTask(object sender, DoWorkEventArgs e) { var winProgress = new MainWindowStatusNotify() { alertLevel = AlertLevel.RUN, animateProgress = true, progressDuration = 100, nowProgress = 0 }; var isTaskStop = false; //当前执行的队列编号 var curTaskNo = 1; //当前队列最大元素数量 var maxedQueueCount = mSyncTaskQueue.Count; while (mSyncTaskQueue.Count > 0) { var arg = new DeviceSyncTaskArgs(); var isPick = mSyncTaskQueue.TryDequeue(out arg); if (!isPick) { continue; } //每个队列的分片进度 double eachBaseProgress = 0; //筛选WPD设备 using (var device = arg.Device) { device.Connect(); //检查目标文件夹是否存在, 不存在则创建 if (!Directory.Exists(arg.Item.PcFolderNameView)) { FileUtil.CreateFolder(arg.Item.PcFolderNameView); } var targetMobileFolder = Path.Combine(arg.DevicePath, arg.Item.MobileFolderNameView); if (!device.DirectoryExists(targetMobileFolder)) { device.CreateDirectory(targetMobileFolder); } //将两端的文件合并到一个集合 var allItems = arg.Item.PcItemList.ToList(); allItems.AddRange(arg.Item.MobileItemList.ToList()); var fileCounter = 1; allItems.ForEach((syncFile) => { //检查任务是否取消 if (mSyncFileBgWorker.CancellationPending) { isTaskStop = true; return; } //如果队列数量增大超过初始量, 记录一个最大数量 if (mSyncTaskQueue.Count + 1 > maxedQueueCount) { maxedQueueCount = mSyncTaskQueue.Count + 1; } //当前队列数量=此队列拥有过的最大数量 var curQueueCount = maxedQueueCount; //计算每个队列的分片进度 eachBaseProgress = 100 / curQueueCount; //每个队列的基础进度, 从0开始, 以分片进度递增 double baseProgress = 100 - (mSyncTaskQueue.Count + 1) * eachBaseProgress; //计算每个文件的分片进度 double eachFileProgress = eachBaseProgress / allItems.Count; //文件的执行进度, 从0开始递增 double fileProgress = eachFileProgress * fileCounter - eachFileProgress; //总进度 = 基础进度 + 文件进度 WindowUtil.CalcProgress(winProgress, string.Format("主任务 [{0}/{1}], 子任务 [{2}/{3}], 正在复制 [ {4} ]", curTaskNo, curQueueCount, fileCounter, allItems.Count, syncFile.NameView), baseProgress + fileProgress); arg.Progress = winProgress; arg.ProgressType = SyncTaskProgressType.SUB_ITEM_FINISH; mSyncFileBgWorker.ReportProgress(0, arg); //执行同步 if (syncFile.SourceView == SyncDeviceType.PC) { //电脑端, 需要复制到移动端 var targetFolder = Path.Combine(arg.DevicePath, arg.Item.MobileFolderNameView); using (FileStream stream = System.IO.File.OpenRead(Path.Combine(arg.Item.PcFolderNameView, syncFile.NameView))) { device.UploadFile(stream, Path.Combine(targetFolder, syncFile.NameView)); } } else { MediaFileInfo fileInfo = device.GetFileInfo(Path.Combine(arg.DevicePath, arg.Item.MobileFolderNameView, syncFile.NameView)); fileInfo.CopyTo(Path.Combine(arg.Item.PcFolderNameView, syncFile.NameView)); } fileProgress = eachFileProgress * fileCounter; WindowUtil.CalcProgress(winProgress, string.Format("主任务 [{0}/{1}], 子任务 [{2}/{3}], 正在复制 [ {4} ]", curTaskNo, curQueueCount, fileCounter, allItems.Count, syncFile.NameView), baseProgress + fileProgress); arg.Source = syncFile.SourceView; arg.DeviceSyncItem = syncFile; mSyncFileBgWorker.ReportProgress(0, arg); fileCounter++; }); device.Disconnect(); } arg.IsOk = !isTaskStop; arg.ProgressType = SyncTaskProgressType.QUEUE_FINISH; if (!isTaskStop) { //一个队列执行完之后, 发送当前队列进度 WindowUtil.CalcProgress(arg.Progress, string.Format("主任务 [{0}/{1}]同步完成", curTaskNo, maxedQueueCount), curTaskNo * eachBaseProgress); mSyncFileBgWorker.ReportProgress(0, arg); curTaskNo++; } else { mSyncFileBgWorker.ReportProgress(0, arg); } } e.Result = !isTaskStop; }