From 324bd31831bcc029209f18909f4bc2c2a2d9fc0e Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Wed, 25 Jan 2023 09:55:56 +0000 Subject: [PATCH 1/3] use BeginUpdate when possible + do not box the items "LoadOcrFixList" --- src/ui/Forms/Options/WordLists.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ui/Forms/Options/WordLists.cs b/src/ui/Forms/Options/WordLists.cs index c261ab88e..ee66af1c4 100644 --- a/src/ui/Forms/Options/WordLists.cs +++ b/src/ui/Forms/Options/WordLists.cs @@ -134,6 +134,11 @@ namespace Nikse.SubtitleEdit.Forms.Options buttonAddUserWord.Enabled = false; buttonRemoveOcrFix.Enabled = false; buttonAddOcrFix.Enabled = false; + + listViewNames.BeginUpdate(); + listBoxUserWordLists.BeginUpdate(); + listBoxOcrFixList.BeginUpdate(); + listViewNames.Items.Clear(); listBoxUserWordLists.Items.Clear(); listBoxOcrFixList.Items.Clear(); @@ -153,6 +158,10 @@ namespace Nikse.SubtitleEdit.Forms.Options LoadNames(language, true); } + + listViewNames.EndUpdate(); + listBoxUserWordLists.EndUpdate(); + listBoxOcrFixList.EndUpdate(); } private void LoadOcrFixList(bool reloadListBox) @@ -167,8 +176,10 @@ namespace Nikse.SubtitleEdit.Forms.Options { listBoxOcrFixList.BeginUpdate(); listBoxOcrFixList.Items.Clear(); - listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.WordReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray()); - listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.PartialLineWordBoundaryReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray()); + // ReSharper disable once CoVariantArrayConversion + listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.WordReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray()); + // ReSharper disable once CoVariantArrayConversion + listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.PartialLineWordBoundaryReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray()); listBoxOcrFixList.Sorted = true; listBoxOcrFixList.EndUpdate(); } @@ -182,8 +193,10 @@ namespace Nikse.SubtitleEdit.Forms.Options if (reloadListBox) { + listBoxUserWordLists.BeginUpdate(); listBoxUserWordLists.Items.Clear(); listBoxUserWordLists.Items.AddRange(_userWordList.ToArray()); + listBoxUserWordLists.BeginUpdate(); } } From 885bb6fe0ac5bafa15d3d9bf654d6bc15e7f27d6 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 28 Jan 2023 11:54:10 +0000 Subject: [PATCH 2/3] Make namelist more interfactive by using async/await --- src/libse/Dictionaries/NameList.cs | 6 +++ src/ui/Forms/Options/WordLists.cs | 65 ++++++++++++++++-------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/libse/Dictionaries/NameList.cs b/src/libse/Dictionaries/NameList.cs index 046d408b4..84987f16f 100644 --- a/src/libse/Dictionaries/NameList.cs +++ b/src/libse/Dictionaries/NameList.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using System.Xml; namespace Nikse.SubtitleEdit.Core.Dictionaries @@ -316,5 +317,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries } return false; } + + public static async Task CreateAsync(string dictionaryFolder, string languageName, bool useOnlineNameList, string namesUrl) + { + return await Task.Run(() => new NameList(dictionaryFolder, languageName, useOnlineNameList, namesUrl)); + } } } diff --git a/src/ui/Forms/Options/WordLists.cs b/src/ui/Forms/Options/WordLists.cs index ee66af1c4..40edec558 100644 --- a/src/ui/Forms/Options/WordLists.cs +++ b/src/ui/Forms/Options/WordLists.cs @@ -126,7 +126,7 @@ namespace Nikse.SubtitleEdit.Forms.Options } } - private void ComboBoxWordListLanguageSelectedIndexChanged(object sender, EventArgs e) + private async void ComboBoxWordListLanguageSelectedIndexChanged(object sender, EventArgs e) { buttonRemoveNameEtc.Enabled = false; buttonAddNames.Enabled = false; @@ -156,7 +156,7 @@ namespace Nikse.SubtitleEdit.Forms.Options // OCR fix words LoadOcrFixList(true); - LoadNames(language, true); + await LoadNamesAsync(language, true); } listViewNames.EndUpdate(); @@ -200,33 +200,31 @@ namespace Nikse.SubtitleEdit.Forms.Options } } - private void LoadNames(string language, bool reloadListBox) + private async Task LoadNamesAsync(string language, bool reloadListBox) { - var task = Task.Factory.StartNew(() => - { - // names etc - var nameList = new NameList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl); - _wordListNames = nameList.GetAllNames(); - _wordListNames.Sort(); - return _wordListNames; - }); - + // update all names + _wordListNames = await GetNamesSortedFromSourceAsync().ConfigureAwait(true); + if (reloadListBox) { - // reload the listbox on a continuation ui thead - var uiContext = TaskScheduler.FromCurrentSynchronizationContext(); - task.ContinueWith(originalTask => + listViewNames.BeginUpdate(); + listViewNames.Items.Clear(); + var list = new List(); + foreach (var item in _wordListNames) { - listViewNames.BeginUpdate(); - listViewNames.Items.Clear(); - var list = new List(); - foreach (var item in originalTask.Result) - { - list.Add(new ListViewItem(item)); - } - listViewNames.Items.AddRange(list.ToArray()); - listViewNames.EndUpdate(); - }, uiContext); + list.Add(new ListViewItem(item)); + } + listViewNames.Items.AddRange(list.ToArray()); + listViewNames.EndUpdate(); + } + + async Task> GetNamesSortedFromSourceAsync() + { + var nameList = await NameList.CreateAsync(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl) + .ConfigureAwait(false); + var names = nameList.GetAllNames(); + names.Sort(); + return names; } } @@ -242,7 +240,7 @@ namespace Nikse.SubtitleEdit.Forms.Options return cb?.CultureInfo.Name.Replace('-', '_'); } - private void ButtonAddNamesClick(object sender, EventArgs e) + private async void ButtonAddNamesClick(object sender, EventArgs e) { var languageIndex = comboBoxWordListLanguage.SelectedIndex; if (languageIndex < 0) @@ -254,9 +252,13 @@ namespace Nikse.SubtitleEdit.Forms.Options var text = textBoxNameEtc.Text.RemoveControlCharacters().Trim(); if (!string.IsNullOrEmpty(language) && text.Length > 1 && !_wordListNames.Contains(text)) { - var nameList = new NameList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl); + // adds new name + var nameList = await NameList.CreateAsync(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl); nameList.Add(text); - LoadNames(language, true); + + // reload + await LoadNamesAsync(language, true).ConfigureAwait(true); + labelStatus.Text = string.Format(LanguageSettings.Current.Settings.WordAddedX, text); textBoxNameEtc.Text = string.Empty; textBoxNameEtc.Focus(); @@ -288,7 +290,7 @@ namespace Nikse.SubtitleEdit.Forms.Options buttonRemoveNameEtc.Enabled = listViewNames.SelectedItems.Count >= 1; } - private void ButtonRemoveNameEtcClick(object sender, EventArgs e) + private async void ButtonRemoveNameEtcClick(object sender, EventArgs e) { if (listViewNames.SelectedItems.Count == 0) { @@ -314,7 +316,8 @@ namespace Nikse.SubtitleEdit.Forms.Options if (result == DialogResult.Yes) { var removeCount = 0; - var namesList = new NameList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl); + var namesList = await NameList.CreateAsync(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl) + .ConfigureAwait(true); for (var idx = listViewNames.SelectedIndices.Count - 1; idx >= 0; idx--) { index = listViewNames.SelectedIndices[idx]; @@ -326,7 +329,7 @@ namespace Nikse.SubtitleEdit.Forms.Options if (removeCount > 0) { - LoadNames(language, true); // reload + await LoadNamesAsync(language, true).ConfigureAwait(true); // reload if (index < listViewNames.Items.Count) { From 35e9c7b78ef2de232e2ada35cc0455ee22c76b89 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 28 Jan 2023 11:56:40 +0000 Subject: [PATCH 3/3] Fixes wrong method call --- src/ui/Forms/Options/WordLists.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/Forms/Options/WordLists.cs b/src/ui/Forms/Options/WordLists.cs index 40edec558..d31f43f16 100644 --- a/src/ui/Forms/Options/WordLists.cs +++ b/src/ui/Forms/Options/WordLists.cs @@ -196,7 +196,7 @@ namespace Nikse.SubtitleEdit.Forms.Options listBoxUserWordLists.BeginUpdate(); listBoxUserWordLists.Items.Clear(); listBoxUserWordLists.Items.AddRange(_userWordList.ToArray()); - listBoxUserWordLists.BeginUpdate(); + listBoxUserWordLists.EndUpdate(); } }