public override async Task RunTask() { base.RunTask(); if (!destinationDeleteResult) { fileCopyResult = false; return; } Logging.Info("Copying file from location {0} to location {1}", SourceFilePath, DestinationFilePath); copyProgress = new Progress <RelhaxProgress>(); cancellationTokenSource = new CancellationTokenSource(); fileCopier = new FileCopier(SourceFilePath, DestinationFilePath) { CancellationToken = cancellationTokenSource.Token, Reporter = copyProgress }; if (DatabaseAutomationRunner != null) { copyProgress.ProgressChanged += DatabaseAutomationRunner.RelhaxProgressChanged; } try { fileCopyResult = await fileCopier.CopyFileAsync(); } catch (OperationCanceledException) { Logging.Info("The copy operation was canceled"); fileCopyResult = false; } catch (Exception ex) { Logging.Exception(ex.ToString()); fileCopyResult = false; } finally { cancellationTokenSource.Dispose(); if (DatabaseAutomationRunner != null) { copyProgress.ProgressChanged -= DatabaseAutomationRunner.RelhaxProgressChanged; } } }
public async override Task RunTask() { await base.RunTask(); if (searchResults == null || searchResults.Count() == 0) { return; } if (!Directory.Exists(DestinationPath)) { Logging.Debug("DestinationPath {0} does not exist, create", DestinationPath); Directory.CreateDirectory(DestinationPath); } cancellationTokenSource = new CancellationTokenSource(); if (reportingProgress) { relhaxProgress = new RelhaxProgress() { ChildCurrentProgress = "barChildTextParent", ChildCurrent = 0, ChildTotal = 0, ParrentCurrent = 0, ParrentTotal = 0 }; progress = new Progress <RelhaxProgress>(); progress.ProgressChanged += DatabaseAutomationRunner.RelhaxProgressChanged; } await Task.Run(async() => { try { fileCopier = new FileCopier(relhaxProgress) { CancellationToken = this.cancellationTokenSource.Token, Reporter = this.progress }; if (reportingProgress) { relhaxProgress.ParrentTotal = searchResults.Count(); } //copy each file over foreach (string sourceFile in searchResults) { if (reportingProgress) { relhaxProgress.ParrentCurrent++; (progress as IProgress <RelhaxProgress>).Report(relhaxProgress); } string destinationFile = sourceFile.Replace(DirectoryPath, DestinationPath); string destinationPath = Path.GetDirectoryName(destinationFile); if (!Directory.Exists(destinationPath)) { Directory.CreateDirectory(destinationPath); } bool result = await fileCopier.CopyFileAsync(sourceFile, destinationFile); if (!result) { return; } } if (reportingProgress) { relhaxProgress.ParrentTotal = relhaxProgress.ParrentCurrent; (progress as IProgress <RelhaxProgress>).Report(relhaxProgress); } good = true; } catch (OperationCanceledException) { good = false; return; } catch (Exception ex) { Logging.Exception(ex.ToString()); good = false; return; } finally { cancellationTokenSource.Dispose(); if (reportingProgress) { progress.ProgressChanged -= DatabaseAutomationRunner.RelhaxProgressChanged; } } }); }
protected Task <string> BuildTask(string filePath) { var progress = new FileCopyProgress(ReportProgress); return(FileCopier.CopyFileAsync(filePath, PathService.Destination, progress, CancellationTokenSource.Token)); }