public async ValueTask Handle(IBackgroundTaskContext context)
        {
            if (context.BackgroundTask is not ImageFileVerifyBackgroundTask verifyBackgroundTask)
            {
                return;
            }

            try
            {
                var commandHelper = new CommandHelper();
                var verifyCommand =
                    new VerifyCommand(loggerFactory.CreateLogger <VerifyCommand>(), commandHelper,
                                      Enumerable.Empty <IPhysicalDrive>(), verifyBackgroundTask.SourcePath,
                                      verifyBackgroundTask.DestinationPath);
                verifyCommand.DataProcessed += async(_, args) =>
                {
                    await progressHubContext.SendProgress(new Progress
                    {
                        Title               = verifyBackgroundTask.Title,
                        IsComplete          = false,
                        PercentComplete     = args.PercentComplete,
                        BytesProcessed      = args.BytesProcessed,
                        BytesRemaining      = args.BytesRemaining,
                        BytesTotal          = args.BytesTotal,
                        MillisecondsElapsed = args.PercentComplete > 0
                            ? (long)args.TimeElapsed.TotalMilliseconds
                            : new long?(),
                        MillisecondsRemaining = args.PercentComplete > 0
                            ? (long)args.TimeRemaining.TotalMilliseconds
                            : new long?(),
                        MillisecondsTotal = args.PercentComplete > 0
                            ? (long)args.TimeTotal.TotalMilliseconds
                            : new long?()
                    }, context.Token);
                };

                var result = await verifyCommand.Execute(context.Token);

                await progressHubContext.SendProgress(new Progress
                {
                    Title           = verifyBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = result.IsFaulted,
                    ErrorMessage    = result.IsFaulted ? result.Error.Message : null,
                    PercentComplete = 100
                }, context.Token);
            }
            catch (Exception e)
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = verifyBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = true,
                    ErrorMessage    = e.Message,
                    PercentComplete = 100
                }, context.Token);
            }
        }
        public async ValueTask Handle(IBackgroundTaskContext context)
        {
            if (context.BackgroundTask is not BlankBackgroundTask blankBackgroundTask)
            {
                return;
            }

            try
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = blankBackgroundTask.Title,
                    IsComplete      = false,
                    PercentComplete = 50,
                }, context.Token);

                var commandHelper = new CommandHelper();
                var blankCommand  = new BlankCommand(loggerFactory.CreateLogger <BlankCommand>(), commandHelper, blankBackgroundTask.Path,
                                                     blankBackgroundTask.CompatibleSize ? Convert.ToInt64(blankBackgroundTask.Size * 0.95) : blankBackgroundTask.Size);

                var result = await blankCommand.Execute(context.Token);

                await Task.Delay(1000, context.Token);

                await progressHubContext.SendProgress(new Progress
                {
                    Title           = blankBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = result.IsFaulted,
                    ErrorMessage    = result.IsFaulted ? result.Error.Message : null,
                    PercentComplete = 100
                }, context.Token);
            }
            catch (Exception e)
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = blankBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = true,
                    ErrorMessage    = e.Message,
                    PercentComplete = 100
                }, context.Token);
            }
        }
Esempio n. 3
0
        public async ValueTask Handle(IBackgroundTaskContext context)
        {
            if (context.BackgroundTask is not OptimizeBackgroundTask optimizeBackgroundTask)
            {
                return;
            }

            try
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = optimizeBackgroundTask.Title,
                    IsComplete      = false,
                    PercentComplete = 50,
                }, context.Token);

                var commandHelper   = new CommandHelper();
                var optimizeCommand = new OptimizeCommand(loggerFactory.CreateLogger <OptimizeCommand>(), commandHelper, optimizeBackgroundTask.Path);

                var result = await optimizeCommand.Execute(context.Token);

                await Task.Delay(1000, context.Token);

                await progressHubContext.SendProgress(new Progress
                {
                    Title           = optimizeBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = result.IsFaulted,
                    ErrorMessage    = result.IsFaulted ? result.Error.Message : null,
                    PercentComplete = 100
                }, context.Token);
            }
            catch (Exception e)
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = optimizeBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = true,
                    ErrorMessage    = e.Message,
                    PercentComplete = 100
                }, context.Token);
            }
        }