コード例 #1
0
        public static void Setup()
        {
            TypeMap.Register.Singleton <IRuntimeEnvironment>(() => new FakeRuntimeEnvironment());

            _progressMock     = new Mock <IProgressContext>();
            _singleThreadMock = new Mock <ISingleThread>();

            _progress = new WorkerGroupProgressContext(_progressMock.Object, _singleThreadMock.Object);
        }
コード例 #2
0
        private async Task InvokeAsync <T>(IEnumerable <T> files, Func <T, IProgressContext, Task <FileOperationContext> > workAsync, Func <FileOperationContext, Task> allCompleteAsync)
        {
            WorkerGroupProgressContext groupProgress = new WorkerGroupProgressContext(new CancelProgressContext(new ProgressContext()), New <ISingleThread>());

            await New <IProgressBackground>().WorkAsync(nameof(DoFilesAsync),
                                                        async(IProgressContext progress) =>
            {
                progress.NotifyLevelStart();

                FileOperationContext result = await Task.Run(async() =>
                {
                    FileOperationContext context = null;

                    foreach (T file in files)
                    {
                        try
                        {
                            context = await workAsync(file, progress);
                        }
                        catch (Exception ex) when(ex is OperationCanceledException)
                        {
                            return(new FileOperationContext(file.ToString(), ErrorStatus.Canceled));
                        }
                        catch (Exception ex) when(ex is AxCryptException)
                        {
                            AxCryptException ace = ex as AxCryptException;
                            New <IReport>().Exception(ace);
                            return(new FileOperationContext(ace.DisplayContext.Default(file), ace.InnerException?.Message ?? ace.Message, ace.ErrorStatus));
                        }
                        catch (Exception ex)
                        {
                            New <IReport>().Exception(ex);
                            return(new FileOperationContext(file.ToString(), ex.Message, ErrorStatus.Exception));
                        }
                        if (context.ErrorStatus != ErrorStatus.Success)
                        {
                            return(context);
                        }
                        progress.Totals.AddFileCount(1);
                    }
                    return(new FileOperationContext(progress.Totals));
                });
                progress.NotifyLevelFinished();
                return(result);
            },
                                                        (FileOperationContext status) => allCompleteAsync(status),
                                                        groupProgress).Free();
        }