コード例 #1
0
        public void Start()
        {
            try
            {
                sourceToken = new CancellationTokenSource();
                token       = sourceToken.Token;
                Atf         = new AnalysisTextFiles();
                SetAnalysisWords();

                IsReplacingFiles = null;
                IsAnalysingFiles = null;
                IsCopyingFiles   = null;
                IsStoped         = false;
                IsPaused         = false;
                allTXTFiles.Clear();

                IsSearchingFiles    = true;
                AnalysisProgress    = 0;
                CopyingProgress     = 0;
                ReplacementProgress = 0;
                float percent = 0;
                Task  task1   = Task.Factory.StartNew(() => TraverseTree(SourceFolderPath), token)
                                .ContinueWith(result =>
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        isSearchingFiles = false;
                        IsAnalysingFiles = true;
                        percent          = 100f / allTXTFiles.Count;
                    }));
                }, token);
                Task task2 = task1.ContinueWith(result => Parallel.ForEach(allTXTFiles, (item) =>
                {
                    atf.Analysis(item, mres, token);

                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        if (AnalysisProgress + percent < 100)
                        {
                            AnalysisProgress += percent;
                        }
                        else
                        {
                            AnalysisProgress = 100;
                        }
                    }));
                }), token).ContinueWith(result =>
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        AnalysisProgress = 100;
                        isAnalysingFiles = false;
                        IsCopyingFiles   = true;
                        percent          = 100f / allTXTFiles.Where(a => a.CountWords > 0).Count() / 2f;
                    }));
                }, token);


                Task task3 = task2.ContinueWith(result => Parallel.ForEach(allTXTFiles, (item) =>
                {
                    CopyFile(item, "(Original)");

                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        CountReplacedWords += item.CountWords;
                        if (CopyingProgress + percent < 50)
                        {
                            CopyingProgress += percent;
                        }
                        else
                        {
                            CopyingProgress = 50;
                        }
                    }));
                }), token);

                Task task4 = task3.ContinueWith(result => Parallel.ForEach(allTXTFiles, (item) =>
                {
                    CopyFile(item, "(Replace)");
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        if (CopyingProgress + percent < 100)
                        {
                            CopyingProgress += percent;
                        }
                        else
                        {
                            CopyingProgress = 100;
                        }
                    }));
                }), token)
                             .ContinueWith(result =>
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        CopyingProgress  = 100;
                        percent          = 100f / allTXTFiles.Where(a => a.CountWords > 0).Count();
                        isCopyingFiles   = false;
                        IsReplacingFiles = true;
                    }));
                }, token);

                Task task5 = task4.ContinueWith(result => Parallel.ForEach(allTXTFiles, (item) =>
                {
                    atf.Replace(item, mres);
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        if (ReplacementProgress + percent < 100)
                        {
                            ReplacementProgress += percent;
                        }
                        else
                        {
                            ReplacementProgress = 100;
                        }
                    }));
                }), token)
                             .ContinueWith(result =>
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        ReplacementProgress = 100;
                        IsReplacingFiles    = false;
                        IsStoped            = true;
                    }));
                }, token);

                Task task6 = task5.ContinueWith(result => Parallel.Invoke(WriteOrder), token);
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        public ViewModel()
        {
            Mutex mutex = null;

            if (!Mutex.TryOpenExisting("ExamWork", out mutex))
            {
                mutex = new Mutex(true, "ExamWork");
            }
            else
            {
                MessageBox.Show("Only one copy can be opened."); App.Current.Shutdown();
            }

            selectForbiddenWordsCommand = new DelegateCommand(SelectFileWithForbiddenWords);
            selectResultFolderCommand   = new DelegateCommand(SelectSaveFolderSource);
            addNewForbiddenWordCommand  = new DelegateCommand(AddNewForbiddenWord, () => !String.IsNullOrWhiteSpace(NewForbiddenWord));
            removeForbiddenWordsCommand = new DelegateCommand(RemoveForbiddenWord, () => SelectedFolbiddenWord != null);
            selectSourceFolderCommand   = new DelegateCommand(SelectFolderSource);

            startCommand  = new DelegateCommand(Start, () => !String.IsNullOrWhiteSpace(ResultFolderPath) && !String.IsNullOrWhiteSpace(SourceFolderPath) && (IsStoped || IsAnalysingFiles != true && IsSearchingFiles != true && IsCopyingFiles != true && IsReplacingFiles != true));;
            pauseCommand  = new DelegateCommand(Pause, () => (!IsStoped && !IsPaused) && (IsAnalysingFiles == true || IsSearchingFiles == true || IsCopyingFiles == true || IsReplacingFiles == true));
            resumeCommand = new DelegateCommand(Resume, () => IsPaused && !IsStoped);
            stopCommand   = new DelegateCommand(Stop, () => !IsStoped && !IsPaused && (IsAnalysingFiles == true || IsSearchingFiles == true || IsCopyingFiles == true || IsReplacingFiles == true));

            PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == nameof(NewForbiddenWord))
                {
                    addNewForbiddenWordCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(SelectedFolbiddenWord))
                {
                    removeForbiddenWordsCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(ResultFolderPath))
                {
                    startCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(SourceFolderPath))
                {
                    startCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(ForbiddenWords))
                {
                    startCommand.RaiseCanExecuteChanged();
                }

                else if (args.PropertyName == nameof(IsStoped))
                {
                    startCommand.RaiseCanExecuteChanged();
                    resumeCommand.RaiseCanExecuteChanged();
                    pauseCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(IsPaused))
                {
                    resumeCommand.RaiseCanExecuteChanged();
                    pauseCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(IsAnalysingFiles))
                {
                    startCommand.RaiseCanExecuteChanged();
                    pauseCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(IsSearchingFiles))
                {
                    startCommand.RaiseCanExecuteChanged();
                    pauseCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(IsCopyingFiles))
                {
                    startCommand.RaiseCanExecuteChanged();
                    pauseCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
                else if (args.PropertyName == nameof(IsReplacingFiles))
                {
                    startCommand.RaiseCanExecuteChanged();
                    pauseCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
            };
            atf              = new AnalysisTextFiles();
            mres             = new ManualResetEventSlim(true);
            sourceToken      = new CancellationTokenSource();
            token            = sourceToken.Token;
            IsSearchingFiles = null;
            IsReplacingFiles = null;
            IsAnalysingFiles = null;
            IsCopyingFiles   = null;
            IsStoped         = false;
            IsPaused         = false;
        }