From 0b1b6b7ddb04fdab249f4908dee9d6f8717490c9 Mon Sep 17 00:00:00 2001 From: OmrSi Date: Tue, 10 Nov 2020 05:07:16 +0200 Subject: [PATCH] Use Task.Run and await in getting chapters --- src/Forms/Main.cs | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 9851d53a3..31dbe8b80 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -27084,42 +27084,35 @@ namespace Nikse.SubtitleEdit.Forms } } - private void toolStripMenuItemImportChapters_Click(object sender, EventArgs e) + private async void toolStripMenuItemImportChapters_Click(object sender, EventArgs e) { toolStripMenuItemImportChapters.Enabled = false; ShowStatus(_language.ImportingChapters); var chaps = new List(); - var getChapters = System.Threading.Tasks.Task.Factory.StartNew(() => + using (var matroska = new MatroskaFile(VideoFileName)) { - using (var matroska = new MatroskaFile(VideoFileName)) - { - chaps = matroska.GetChapters(); - } - }); + chaps = await System.Threading.Tasks.Task.Run(() => matroska.GetChapters()); + } - var uiContext = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext(); - getChapters.ContinueWith(_ => + if (chaps?.Count > 0) { - if (chaps?.Count > 0) - { - mediaPlayer.Chapters = chaps; + mediaPlayer.Chapters = chaps; - if (audioVisualizer.WavePeaks != null) - { - audioVisualizer.Chapters = chaps; - } - - ShowStatus(string.Format(_language.XChaptersImported, chaps?.Count)); - } - else + if (audioVisualizer.WavePeaks != null) { - ShowStatus(_language.NoChapters); - MessageBox.Show(_language.NoChapters, Title); + audioVisualizer.Chapters = chaps; } - toolStripMenuItemImportChapters.Enabled = true; - }, uiContext); + ShowStatus(string.Format(_language.XChaptersImported, chaps?.Count)); + } + else + { + ShowStatus(_language.NoChapters); + MessageBox.Show(_language.NoChapters, Title); + } + + toolStripMenuItemImportChapters.Enabled = true; } private void toolStripMenuItemImportSceneChanges_Click(object sender, EventArgs e)