From 9e6992acd28923f75b22fcc6ba908fbe223f3654 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 23 Aug 2020 15:49:22 +0100 Subject: [PATCH] Changed: thread helper --- MainForm.cs | 24 +------- RyzStudio/Windows/Forms/ThreadControl.cs | 50 ++++++++++++++++ Windows/Forms/BookmarkTreeView.cs | 72 ++---------------------- 3 files changed, 58 insertions(+), 88 deletions(-) diff --git a/MainForm.cs b/MainForm.cs index 5704146..e4fbe77 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -822,17 +822,7 @@ namespace bzit.bomg this.ApplicationMode = AppMode.Open; } - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name; - })); - } - else - { - this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name; - } + RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); } protected void loadBookmarkFile_ForRYZ(string filename, string password = "") @@ -977,17 +967,7 @@ namespace bzit.bomg this.ApplicationMode = AppMode.New; } - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name; - })); - } - else - { - this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name; - } + RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); } protected void openBookmark(TreeNode node) diff --git a/RyzStudio/Windows/Forms/ThreadControl.cs b/RyzStudio/Windows/Forms/ThreadControl.cs index b63586b..13581da 100644 --- a/RyzStudio/Windows/Forms/ThreadControl.cs +++ b/RyzStudio/Windows/Forms/ThreadControl.cs @@ -71,6 +71,56 @@ namespace RyzStudio.Windows.Forms return n; } + public static TreeNode Add(TreeNode control, string key, string text, int imageIndex, int selectedImageIndex) + { + TreeNode rs = null; + + if (control.TreeView.InvokeRequired) + { + control.TreeView.Invoke(new MethodInvoker(() => { + rs = control.Nodes.Add(key, text, imageIndex, selectedImageIndex); + })); + } + else + { + rs = control.Nodes.Add(key, text, imageIndex, selectedImageIndex); + } + + return rs; + } + + public static TreeNode Add(TreeView control, string key, string text, int imageIndex, int selectedImageIndex) + { + TreeNode rs = null; + + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + rs = control.Nodes.Add(key, text, imageIndex, selectedImageIndex); + })); + } + else + { + rs = control.Nodes.Add(key, text, imageIndex, selectedImageIndex); + } + + return rs; + } + + public static void Add(Control parentControl, ImageList control, string key, Image value) + { + if (parentControl.InvokeRequired) + { + parentControl.Invoke(new MethodInvoker(() => { + control.Images.Add(key, value); + })); + } + else + { + control.Images.Add(key, value); + } + } + public static int Add(TreeNode control, TreeNode value) { int n = -1; diff --git a/Windows/Forms/BookmarkTreeView.cs b/Windows/Forms/BookmarkTreeView.cs index 50c4091..6c9cbba 100644 --- a/Windows/Forms/BookmarkTreeView.cs +++ b/Windows/Forms/BookmarkTreeView.cs @@ -166,17 +166,7 @@ namespace RyzStudio.Windows.Forms TreeNode tn2 = addFolderPath(viewModel.TreeviewPath); - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - tn2.Nodes.Add(tn); - })); - } - else - { - tn2.Nodes.Add(tn); - } + ThreadControl.Add(tn2, tn); this.HasChanged = true; } @@ -217,17 +207,7 @@ namespace RyzStudio.Windows.Forms public void Clear() { - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.Nodes.Clear(); - })); - } - else - { - this.Nodes.Clear(); - } + ThreadControl.Clear(this); this.HasChanged = true; } @@ -666,17 +646,7 @@ namespace RyzStudio.Windows.Forms } else { - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.ImageList.Images.Add(model.SiteAddress, rs); - })); - } - else - { - this.ImageList.Images.Add(model.SiteAddress, rs); - } + ThreadControl.Add(this, this.ImageList, model.SiteAddress, rs); return this.ImageList.Images.IndexOfKey(model.SiteAddress); } @@ -689,17 +659,7 @@ namespace RyzStudio.Windows.Forms return (int)IconSet.Default; } - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.ImageList.Images.Add(model.SiteAddress, bmp); - })); - } - else - { - this.ImageList.Images.Add(model.SiteAddress, bmp); - } + ThreadControl.Add(this, this.ImageList, model.SiteAddress, bmp); iconDatabase.AddIcon(model.SiteAddress, rawData); @@ -739,17 +699,7 @@ namespace RyzStudio.Windows.Forms { if (!this.Nodes.ContainsKey(item)) { - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.Nodes.Add(item, decodePath(item), (int)IconSet.Root, (int)IconSet.Root); - })); - } - else - { - this.Nodes.Add(item, decodePath(item), (int)IconSet.Root, (int)IconSet.Root); - } + ThreadControl.Add(this, item, decodePath(item), (int)IconSet.Root, (int)IconSet.Root); } tn = this.Nodes[item]; @@ -758,17 +708,7 @@ namespace RyzStudio.Windows.Forms { if (!tn.Nodes.ContainsKey(item)) { - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - tn.Nodes.Add(item, decodePath(item), (int)IconSet.Folder1, (int)IconSet.Folder2); - })); - } - else - { - tn.Nodes.Add(item, decodePath(item), (int)IconSet.Folder1, (int)IconSet.Folder2); - } + ThreadControl.Add(tn, item, decodePath(item), (int)IconSet.Folder1, (int)IconSet.Folder2); } tn = tn.Nodes[item];