From 8615e1421faf5abf9d417c4efc9a3ecd56522676 Mon Sep 17 00:00:00 2001 From: Genteure Date: Sun, 2 May 2021 21:34:27 +0800 Subject: [PATCH] ToolBox: Add cancel --- BililiveRecorder.ToolBox/Commands/Analyze.cs | 16 ++++-- BililiveRecorder.ToolBox/Commands/Export.cs | 16 ++++-- BililiveRecorder.ToolBox/Commands/Fix.cs | 19 +++++-- BililiveRecorder.ToolBox/ResponseStatus.cs | 1 + .../Controls/AutoFixProgressDialog.xaml | 6 ++- .../Controls/AutoFixProgressDialog.xaml.cs | 15 ++++++ BililiveRecorder.WPF/Pages/RootPage.xaml | 3 +- .../Pages/ToolboxAutoFixPage.xaml | 2 +- .../Pages/ToolboxAutoFixPage.xaml.cs | 49 +++++++++++++------ 9 files changed, 96 insertions(+), 31 deletions(-) diff --git a/BililiveRecorder.ToolBox/Commands/Analyze.cs b/BililiveRecorder.ToolBox/Commands/Analyze.cs index 6e345a0..f0343c9 100644 --- a/BililiveRecorder.ToolBox/Commands/Analyze.cs +++ b/BililiveRecorder.ToolBox/Commands/Analyze.cs @@ -4,6 +4,7 @@ using System.IO; using System.IO.Compression; using System.IO.Pipelines; using System.Linq; +using System.Threading; using System.Threading.Tasks; using BililiveRecorder.Flv; using BililiveRecorder.Flv.Amf; @@ -48,9 +49,9 @@ namespace BililiveRecorder.ToolBox.Commands { private static readonly ILogger logger = Log.ForContext(); - public Task> Handle(AnalyzeRequest request) => this.Handle(request, null); + public Task> Handle(AnalyzeRequest request) => this.Handle(request, default, null); - public async Task> Handle(AnalyzeRequest request, Func? progress) + public async Task> Handle(AnalyzeRequest request, CancellationToken cancellationToken, Func? progress) { FileStream? flvFileStream = null; try @@ -109,9 +110,9 @@ namespace BililiveRecorder.ToolBox.Commands await Task.Run(async () => { var count = 0; - while (true) + while (!cancellationToken.IsCancellationRequested) { - var group = await grouping.ReadGroupAsync(default).ConfigureAwait(false); + var group = await grouping.ReadGroupAsync(cancellationToken).ConfigureAwait(false); if (group is null) break; @@ -136,6 +137,9 @@ namespace BililiveRecorder.ToolBox.Commands } }).ConfigureAwait(false); + if (cancellationToken.IsCancellationRequested) + return new CommandResponse { Status = ResponseStatus.Cancelled }; + // Result var response = await Task.Run(() => { @@ -169,6 +173,10 @@ namespace BililiveRecorder.ToolBox.Commands Result = response }; } + catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) + { + return new CommandResponse { Status = ResponseStatus.Cancelled }; + } catch (NotFlvFileException ex) { return new CommandResponse diff --git a/BililiveRecorder.ToolBox/Commands/Export.cs b/BililiveRecorder.ToolBox/Commands/Export.cs index ab9512b..93bbdf4 100644 --- a/BililiveRecorder.ToolBox/Commands/Export.cs +++ b/BililiveRecorder.ToolBox/Commands/Export.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.IO.Pipelines; +using System.Threading; using System.Threading.Tasks; using BililiveRecorder.Flv; using BililiveRecorder.Flv.Parser; @@ -26,9 +27,9 @@ namespace BililiveRecorder.ToolBox.Commands { private static readonly ILogger logger = Log.ForContext(); - public Task> Handle(ExportRequest request) => this.Handle(request, null); + public Task> Handle(ExportRequest request) => this.Handle(request, default, null); - public async Task> Handle(ExportRequest request, Func? progress) + public async Task> Handle(ExportRequest request, CancellationToken cancellationToken, Func? progress) { FileStream? inputStream = null, outputStream = null; try @@ -67,9 +68,9 @@ namespace BililiveRecorder.ToolBox.Commands var tags = new List(); var memoryStreamProvider = new RecyclableMemoryStreamProvider(); using var reader = new FlvTagPipeReader(PipeReader.Create(inputStream), memoryStreamProvider, skipData: true, logger: logger); - while (true) + while (!cancellationToken.IsCancellationRequested) { - var tag = await reader.ReadTagAsync(default).ConfigureAwait(false); + var tag = await reader.ReadTagAsync(cancellationToken).ConfigureAwait(false); if (tag is null) break; tags.Add(tag); @@ -79,6 +80,9 @@ namespace BililiveRecorder.ToolBox.Commands return tags; }); + if (cancellationToken.IsCancellationRequested) + return new CommandResponse { Status = ResponseStatus.Cancelled }; + await Task.Run(() => { using var writer = new StreamWriter(new GZipStream(outputStream, CompressionLevel.Optimal)); @@ -90,6 +94,10 @@ namespace BililiveRecorder.ToolBox.Commands return new CommandResponse { Status = ResponseStatus.OK, Result = new ExportResponse() }; } + catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) + { + return new CommandResponse { Status = ResponseStatus.Cancelled }; + } catch (NotFlvFileException ex) { return new CommandResponse diff --git a/BililiveRecorder.ToolBox/Commands/Fix.cs b/BililiveRecorder.ToolBox/Commands/Fix.cs index e8080c5..aaee342 100644 --- a/BililiveRecorder.ToolBox/Commands/Fix.cs +++ b/BililiveRecorder.ToolBox/Commands/Fix.cs @@ -4,6 +4,7 @@ using System.IO; using System.IO.Compression; using System.IO.Pipelines; using System.Linq; +using System.Threading; using System.Threading.Tasks; using BililiveRecorder.Flv; using BililiveRecorder.Flv.Grouping; @@ -51,9 +52,9 @@ namespace BililiveRecorder.ToolBox.Commands { private static readonly ILogger logger = Log.ForContext(); - public Task> Handle(FixRequest request) => this.Handle(request, null); + public Task> Handle(FixRequest request) => this.Handle(request, default, null); - public async Task> Handle(FixRequest request, Func? progress) + public async Task> Handle(FixRequest request, CancellationToken cancellationToken, Func? progress) { FileStream? flvFileStream = null; try @@ -130,9 +131,9 @@ namespace BililiveRecorder.ToolBox.Commands await Task.Run(async () => { var count = 0; - while (true) + while (!cancellationToken.IsCancellationRequested) { - var group = await grouping.ReadGroupAsync(default).ConfigureAwait(false); + var group = await grouping.ReadGroupAsync(cancellationToken).ConfigureAwait(false); if (group is null) break; @@ -157,6 +158,9 @@ namespace BililiveRecorder.ToolBox.Commands } }).ConfigureAwait(false); + if (cancellationToken.IsCancellationRequested) + return new CommandResponse { Status = ResponseStatus.Cancelled }; + // Post Run if (xmlMode) { @@ -186,6 +190,9 @@ namespace BililiveRecorder.ToolBox.Commands }); } + if (cancellationToken.IsCancellationRequested) + return new CommandResponse { Status = ResponseStatus.Cancelled }; + // Result var response = await Task.Run(() => { @@ -215,6 +222,10 @@ namespace BililiveRecorder.ToolBox.Commands return new CommandResponse { Status = ResponseStatus.OK, Result = response }; } + catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) + { + return new CommandResponse { Status = ResponseStatus.Cancelled }; + } catch (NotFlvFileException ex) { return new CommandResponse diff --git a/BililiveRecorder.ToolBox/ResponseStatus.cs b/BililiveRecorder.ToolBox/ResponseStatus.cs index b8b7c08..ed7ef09 100644 --- a/BililiveRecorder.ToolBox/ResponseStatus.cs +++ b/BililiveRecorder.ToolBox/ResponseStatus.cs @@ -4,6 +4,7 @@ namespace BililiveRecorder.ToolBox { Error = 0, OK, + Cancelled, NotFlvFile, UnknownFlvTagType, InputIOError, diff --git a/BililiveRecorder.WPF/Controls/AutoFixProgressDialog.xaml b/BililiveRecorder.WPF/Controls/AutoFixProgressDialog.xaml index c990993..0b7fd23 100644 --- a/BililiveRecorder.WPF/Controls/AutoFixProgressDialog.xaml +++ b/BililiveRecorder.WPF/Controls/AutoFixProgressDialog.xaml @@ -8,13 +8,15 @@ l:LocalizeDictionary.DesignCulture="" l:ResxLocalizationProvider.DefaultAssembly="BililiveRecorder.WPF" l:ResxLocalizationProvider.DefaultDictionary="Strings" - xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:BililiveRecorder.WPF.Controls" Name="autoFixProgressDialog" - mc:Ignorable="d" + mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> +