diff --git a/src/LogAlligator.App/Controls/FileView.axaml.cs b/src/LogAlligator.App/Controls/FileView.axaml.cs index cee2c03..cd536fb 100644 --- a/src/LogAlligator.App/Controls/FileView.axaml.cs +++ b/src/LogAlligator.App/Controls/FileView.axaml.cs @@ -131,7 +131,7 @@ private async Task LoadFile() ShowLoadingDialog(); await LoadData(); } - catch (TaskCanceledException) + catch (OperationCanceledException) { Log.Information("Loading data was canceled"); RequestRemovalFromView(); diff --git a/src/LogAlligator.App/LineProvider/BufferedFileLineProvider.cs b/src/LogAlligator.App/LineProvider/BufferedFileLineProvider.cs index 5f37ff0..9e72b72 100644 --- a/src/LogAlligator.App/LineProvider/BufferedFileLineProvider.cs +++ b/src/LogAlligator.App/LineProvider/BufferedFileLineProvider.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Avalonia.Threading; namespace LogAlligator.App.LineProvider; @@ -27,18 +28,24 @@ public async Task LoadData(Action progressCallback, CancellationToken token private async Task LoadLinesData(Action progressCallback, CancellationToken token) { const FileOptions fileOptions = FileOptions.SequentialScan | FileOptions.Asynchronous; - await using var stream = new FileStream(path.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, fileOptions); + using var stream = new FileStream(path.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, fileOptions); var reader = new StreamLineReader(stream, 65536); int lineNumber = 1; - while (reader.ReadLine() is { } line) // TODO: Make Async + await Task.Run(() => { - _lines.Add((lineNumber, line.Begin, (int)line.Length)); - lineNumber++; - - if (_lines.Count % 100 == 0) - progressCallback(_lines.Count); - } + while (reader.ReadLine() is { } line) + { + _lines.Add((lineNumber, line.Begin, (int)line.Length)); + lineNumber++; + + if (_lines.Count % 1000 == 0) + { + token.ThrowIfCancellationRequested(); + Dispatcher.UIThread.Post(() => progressCallback(_lines.Count)); + } + } + }, token); progressCallback(_lines.Count); } diff --git a/src/LogAlligator.App/LineProvider/StreamLineReader.cs b/src/LogAlligator.App/LineProvider/StreamLineReader.cs index 1acee92..f7c54bc 100644 --- a/src/LogAlligator.App/LineProvider/StreamLineReader.cs +++ b/src/LogAlligator.App/LineProvider/StreamLineReader.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; namespace LogAlligator.App.LineProvider;