public async ValueTask Handle(IBackgroundTaskContext context) { if (context.BackgroundTask is not ReadBackgroundTask readBackgroundTask) { return; } try { var physicalDrives = await physicalDriveManager.GetPhysicalDrives(); var commandHelper = new CommandHelper(); var readCommand = new ReadCommand(loggerFactory.CreateLogger <ReadCommand>(), commandHelper, physicalDrives, readBackgroundTask.SourcePath, readBackgroundTask.DestinationPath); readCommand.DataProcessed += async(_, args) => { await progressHubConnection.UpdateProgress(new Progress { Title = readBackgroundTask.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 readCommand.Execute(context.Token); await progressHubConnection.UpdateProgress(new Progress { Title = readBackgroundTask.Title, IsComplete = true, HasError = result.IsFaulted, ErrorMessage = result.IsFaulted ? result.Error.Message : null, PercentComplete = 100 }, context.Token); } catch (Exception e) { await progressHubConnection.UpdateProgress(new Progress { Title = readBackgroundTask.Title, IsComplete = true, HasError = true, ErrorMessage = e.Message, PercentComplete = 100 }, context.Token); } }
public async ValueTask Handle(IBackgroundTaskContext context) { if (context.BackgroundTask is not PhysicalDriveInfoBackgroundTask infoBackgroundTask) { return; } var physicalDrives = await physicalDriveManager.GetPhysicalDrives(); var commandHelper = new CommandHelper(); var logger = loggerFactory.CreateLogger <InfoCommand>(); var infoCommand = new InfoCommand(logger, commandHelper, physicalDrives, infoBackgroundTask.Path); infoCommand.DiskInfoRead += async(_, args) => { await resultHubConnection.SendInfoResult(args.MediaInfo.ToViewModel()); }; var result = await infoCommand.Execute(context.Token); if (result.IsFaulted) { await errorHubConnection.UpdateError(result.Error.Message, context.Token); } }
public async ValueTask Handle(IBackgroundTaskContext context) { var physicalDrives = await physicalDriveManager.GetPhysicalDrives(); var commandHelper = new CommandHelper(); var listCommand = new ListCommand(loggerFactory.CreateLogger <ListCommand>(), commandHelper, physicalDrives); listCommand.ListRead += async(_, args) => { await resultHubConnection.SendListResult(args.MediaInfos.Select(x => x.ToViewModel()).ToList()); }; var result = await listCommand.Execute(context.Token); if (result.IsFaulted) { await errorHubConnection.UpdateError(result.Error.Message, context.Token); } }