private void RelhaxProgressReport_ProgressChanged(object sender, RelhaxProgress e)
        {
            if (AutomationTaskProgressBar.Visibility != Visibility.Visible)
            {
                ShowAutomationProgress(e.ChildTotal);
            }

            if (e.ChildCurrent == e.ChildTotal)
            {
                HideAutomationProgress();
            }

            switch (e.ChildCurrentProgress)
            {
            case "barWithTextChild":
                ReportRelhaxProgressBarWithTextChild(e);
                return;

            case "barWithTextParrent":
                ReportRelhaxProgressBarWithTextParrent(e);
                return;

            case "barChildTextParent":
                ReportRelhaxProgressBarChildTextParent(e);
                return;
            }
        }
Beispiel #2
0
        protected virtual async Task <bool> RunFileHashing()
        {
            calculationProgress     = new Progress <RelhaxProgress>();
            cancellationTokenSource = new CancellationTokenSource();
            progress = new RelhaxProgress()
            {
                ChildCurrentProgress = "barWithTextChild", ChildCurrent = 0, ChildTotal = directoryFilesA.Length
            };
            fileHashComparer = new FileHashComparer()
            {
                CancellationTokenA = cancellationTokenSource.Token,
                CancellationTokenB = cancellationTokenSource.Token,
            };

            if (DatabaseAutomationRunner != null)
            {
                calculationProgress.ProgressChanged += DatabaseAutomationRunner.RelhaxProgressChanged;
            }

            try
            {
                for (int i = 0; i < directoryFilesA.Length; i++)
                {
                    await fileHashComparer.ComputeHashA(directoryFilesA[i]);

                    await fileHashComparer.ComputeHashB(directoryFilesB[i]);

                    string fileAHash = fileHashComparer.HashAStringBuilder?.ToString();
                    string fileBHash = fileHashComparer.HashBStringBuilder?.ToString();
                    Logging.Debug("File A hash: {0}", fileAHash);
                    Logging.Debug("File B hash: {0}", fileBHash);

                    AutomationCompareTracker.AddCompare(this, directoryFilesA[i], fileAHash, directoryFilesB[i], fileBHash, AutomationCompareMode.NoMatchContinue);
                    progress.ChildCurrent++;
                    (calculationProgress as IProgress <RelhaxProgress>).Report(progress);

                    if (cancellationTokenSource.Token.IsCancellationRequested)
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                Logging.Exception(ex.ToString());
                return(false);
            }
            finally
            {
                cancellationTokenSource.Dispose();
                if (DatabaseAutomationRunner != null)
                {
                    calculationProgress.ProgressChanged -= DatabaseAutomationRunner.RelhaxProgressChanged;
                }
            }
            return(true);
        }
        protected async Task <bool> ComputeHash(Stream stream, MD5 md5hash, RelhaxProgress progress, IProgress <RelhaxProgress> Reporter, string streamName, StringBuilder builder, CancellationToken cancellationToken)
        {
            using (md5hash = MD5.Create())
            {
                byte[] buffer       = new byte[BYTE_CHUNKS];
                int    numBytesRead = 0;
                progress.ChildTotal   = (int)stream.Length;
                progress.ChildCurrent = 0;
                Reporter?.Report(progress);

                try
                {
                    //we need to use TransformFinalBlock()
                    //for the final calculation rather then TransformBlock
                    while (true)
                    {
                        numBytesRead = await stream.ReadAsync(buffer, 0, BYTE_CHUNKS);

                        progress.ChildCurrent += numBytesRead;

                        if (stream.Position >= stream.Length)
                        {
                            md5hash.TransformFinalBlock(buffer, 0, numBytesRead);
                            break;
                        }
                        else
                        {
                            md5hash.TransformBlock(buffer, 0, numBytesRead, null, 0);
                        }

                        ThrowIfCancellationRequested(cancellationToken);
                        Reporter?.Report(progress);
                    }

                    //output final hash entry and save to Hash property
                    for (int i = 0; i < md5hash.Hash.Length; i++)
                    {
                        builder.Append(md5hash.Hash[i].ToString("x2"));
                    }

                    Logging.Info(LogOptions.ClassName, "Hash for stream {0} calculated to be {1}", stream, builder.ToString());
                    progress.ChildCurrent = progress.ChildTotal;
                    Reporter?.Report(progress);
                }
                catch (OperationCanceledException)
                {
                    Logging.Info("The calculation was canceled");
                    return(false);
                }
                catch (Exception ex)
                {
                    Logging.Exception(ex.ToString());
                    return(false);
                }
            }
            return(true);
        }
 private void ReportRelhaxProgressBarChildTextParent(RelhaxProgress relhaxProgress)
 {
     if (AutomationTaskProgressBar.Maximum != relhaxProgress.ChildTotal)
     {
         AutomationTaskProgressBar.Maximum = relhaxProgress.ChildTotal;
     }
     AutomationTaskProgressBar.Value      = relhaxProgress.ChildCurrent;
     AutomationTaskProgressTextBlock.Text = string.Format("{0} of {1}", relhaxProgress.ParrentCurrent, relhaxProgress.ParrentTotal);
 }
        private async void CleanWorkingDirectoriesButton_Click(object sender, RoutedEventArgs e)
        {
            CancellationTokenSource   tokenSource;
            Progress <RelhaxProgress> reporter = new Progress <RelhaxProgress>();
            RelhaxProgress            progress = new RelhaxProgress();

            AutomationTaskProgressBar.Visibility       = Visibility.Visible;
            AutomationTaskProgressBar.Minimum          = AutomationTaskProgressBar.Maximum = AutomationTaskProgressBar.Value = 0;
            AutomationTaskProgressTextBlock.Visibility = Visibility.Visible;
            AutomationTaskProgressTextBlock.Text       = string.Empty;

            reporter.ProgressChanged += (sender_, args) =>
            {
                if (AutomationTaskProgressBar.Minimum != 0)
                {
                    AutomationTaskProgressBar.Minimum = 0;
                }

                if (AutomationTaskProgressBar.Maximum != args.ChildTotal)
                {
                    AutomationTaskProgressBar.Maximum = args.ChildTotal;
                }

                AutomationTaskProgressBar.Value = args.ChildCurrent;

                AutomationTaskProgressTextBlock.Text = args.ChildCurrentProgress;
            };

            using (tokenSource = new CancellationTokenSource())
            {
                await AutomationSequencer.CleanWorkingDirectoriesAsync(reporter, progress, tokenSource.Token);
            }

            this.Dispatcher.InvokeAsync(async() =>
            {
                AutomationTaskProgressTextBlock.Text = "Done";
                await Task.Delay(2000);
                AutomationTaskProgressBar.Visibility       = Visibility.Hidden;
                AutomationTaskProgressBar.Minimum          = AutomationTaskProgressBar.Maximum = AutomationTaskProgressBar.Value = 0;
                AutomationTaskProgressTextBlock.Visibility = Visibility.Hidden;
                AutomationTaskProgressTextBlock.Text       = string.Empty;
            });
        }
 public FileHashComparer()
 {
     hashProgressA = new RelhaxProgress();
     hashProgressB = new RelhaxProgress();
 }
        private void CleanWorkingDirectories(IProgress <RelhaxProgress> reporter, RelhaxProgress progress, CancellationToken token)
        {
            if (AutomationSequences == null || AutomationSequences.Count == 0)
            {
                return;
            }

            //get the list of files
            List <string> FilesToDelete         = new List <string>();
            List <string> FoldersToGetFilesFrom = AutomationSequences.Select(sequence => sequence.PackageName).ToList();

            foreach (string folder in FoldersToGetFilesFrom)
            {
                string folderPath = Path.Combine(ApplicationConstants.RelhaxTempFolderPath, folder);
                if (Directory.Exists(folderPath))
                {
                    FilesToDelete.AddRange(FileUtils.FileSearch(folderPath, SearchOption.AllDirectories, false, true));
                }

                if (token != null && token.IsCancellationRequested)
                {
                    return;
                }
            }

            if (progress != null)
            {
                progress.ChildTotal   = FilesToDelete.Count;
                progress.ChildCurrent = progress.ParrentCurrent = 0;
                reporter?.Report(progress);
            }


            foreach (string file in FilesToDelete)
            {
                progress.ChildCurrent++;
                progress.ChildCurrentProgress = string.Format("Deleting file {0} of {1}", progress.ChildCurrent, progress.ChildTotal);
                reporter.Report(progress);

                FileUtils.FileDelete(file);
                if (token != null && token.IsCancellationRequested)
                {
                    return;
                }

                if (progress != null)
                {
                    progress.ChildCurrent         = progress.ChildTotal;
                    progress.ChildCurrentProgress = string.Format("Finishing up");
                    reporter?.Report(progress);
                }
            }


            foreach (string folder in FoldersToGetFilesFrom)
            {
                string folderPath = Path.Combine(ApplicationConstants.RelhaxTempFolderPath, folder);
                if (Directory.Exists(folderPath))
                {
                    FileUtils.DirectoryDelete(folderPath, true, true);
                }

                if (token != null && token.IsCancellationRequested)
                {
                    return;
                }
            }
        }
        public async Task CleanWorkingDirectoriesAsync(IProgress <RelhaxProgress> reporter, RelhaxProgress progress, CancellationToken token)
        {
            Logging.Info("Cleaning Working directories start");
            await Task.Run(() => CleanWorkingDirectories(reporter, token : token, progress : progress));

            Logging.Info("Cleaning Working directories finish");
        }
Beispiel #9
0
        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;
                    }
                }
            });
        }
 private void ReportRelhaxProgressBarWithTextParrent(RelhaxProgress relhaxProgress)
 {
     AutomationTaskProgressBar.Value      = relhaxProgress.ParrentCurrent;
     AutomationTaskProgressTextBlock.Text = string.Format("{0} of {1}", relhaxProgress.ParrentCurrent, relhaxProgress.ParrentTotal);
 }
        public async override Task RunTask()
        {
            await base.RunTask();

            if (searchResults == null || searchResults.Count() == 0)
            {
                return;
            }

            cancellationTokenSource = new CancellationTokenSource();

            if (reportingProgress)
            {
                relhaxProgress = new RelhaxProgress()
                {
                    ChildCurrentProgress = "barWithTextChild",
                    ChildCurrent         = 0,
                    ChildTotal           = 0,
                    ParrentCurrent       = 0,
                    ParrentTotal         = 0
                };

                progress = new Progress <RelhaxProgress>();

                progress.ProgressChanged += DatabaseAutomationRunner.RelhaxProgressChanged;
            }

            await Task.Run(() =>
            {
                try
                {
                    if (reportingProgress)
                    {
                        relhaxProgress.ChildTotal = searchResults.Count();
                    }

                    Logging.Debug("Deleting files");
                    foreach (string file in searchResults)
                    {
                        if (reportingProgress)
                        {
                            relhaxProgress.ChildCurrent++;
                            (progress as IProgress <RelhaxProgress>).Report(relhaxProgress);
                        }

                        bool result = true;
                        if (File.Exists(file))
                        {
                            result = FileUtils.FileDelete(file);
                        }
                        if (!result)
                        {
                            return;
                        }
                    }

                    if (reportingProgress)
                    {
                        relhaxProgress.ChildTotal = relhaxProgress.ChildCurrent;
                        (progress as IProgress <RelhaxProgress>).Report(relhaxProgress);
                    }

                    Logging.Debug("Deleting empty directories");
                    FileUtils.ProcessEmptyDirectories(DirectoryPath, true);

                    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;
                    }
                }
            });
        }
Beispiel #12
0
 public FileCopier(RelhaxProgress progress)
 {
     this.progress = progress;
 }
Beispiel #13
0
 public FileCopier()
 {
     progress = new RelhaxProgress()
     {
     };
 }