コード例 #1
0
ファイル: Shell.cs プロジェクト: Kolan92/ExternalSorting
        private void SortFinished(object sender, SortFinishedArgs e)
        {
            if (e.IsSuccess)
            {
                _printer.Print("Sorting finished with success");
            }
            else
            {
                _printer.Print($"Sorting finished with error: {e.Exception}");
            }

            if (_cancellationToken.HasValue)
            {
                _cancellationToken = new Option <CancellationTokenSource>();
            }
        }
コード例 #2
0
        public CancellationTokenSource StartNew(string fileName, Action <SortFinishedArgs> postSortAction)
        {
            var source = new CancellationTokenSource();
            var token  = source.Token;

            var factory = new TaskFactory(token);

            factory.StartNew(() =>
            {
                ISortStrategy strategy = null;

                try
                {
                    token.Register(Thread.CurrentThread.Abort);
                    var memoryChecker   = new MemoryChecker();
                    var strategyFactory = new SortStrategyFactory(memoryChecker);
                    strategy            = strategyFactory.ChooseSortStrategy(fileName);
                    strategy.Sort();
                    postSortAction(new SortFinishedArgs());
                }
                catch (ThreadAbortException)
                {
                    // do nothing, operation was cancelled
                }
                catch (Exception exception)
                {
                    var args = new SortFinishedArgs(exception);
                    postSortAction(args);
                }
                finally
                {
                    strategy.CleanUp();
                }
            }, token);
            return(source);
        }