From 6e4757adeaf750150ccdef9ca890dfd7295738dd Mon Sep 17 00:00:00 2001 From: Stone-Red-Code <56473591+Stone-Red-Code@users.noreply.github.com> Date: Tue, 14 Sep 2021 14:07:08 +0200 Subject: [PATCH] - Exception handling for plugin events - Revert: Execute `OnValueChanged` event in task --- src/DesktopMagic/MainWindow.xaml.cs | 55 ++++++++++++++++++-- src/DesktopMagic/Plugin/PluginWindow.xaml.cs | 35 +++++++------ src/DesktopMagicPlugin.Test/PluginScript.cs | 50 +++++++++++++----- src/DesktopMagicPluginAPI/Inputs/Button.cs | 3 +- src/DesktopMagicPluginAPI/Inputs/Element.cs | 3 +- 5 files changed, 109 insertions(+), 37 deletions(-) diff --git a/src/DesktopMagic/MainWindow.xaml.cs b/src/DesktopMagic/MainWindow.xaml.cs index 4edf8f8..5750cdb 100644 --- a/src/DesktopMagic/MainWindow.xaml.cs +++ b/src/DesktopMagic/MainWindow.xaml.cs @@ -490,7 +490,14 @@ namespace DesktopMagic }; button.Click += (_s, _e) => { - eButton.Click(); + try + { + eButton.Click(); + } + catch (Exception ex) + { + DisplayException(ex.Message); + } }; eButton.OnValueChanged += () => { @@ -513,7 +520,14 @@ namespace DesktopMagic }; checkBox.Click += (_s, _e) => { - eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault(); + try + { + eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault(); + } + catch (Exception ex) + { + DisplayException(ex.Message); + } }; eCheckBox.OnValueChanged += () => { @@ -536,7 +550,14 @@ namespace DesktopMagic }; textBox.TextChanged += (_s, _e) => { - eTextBox.Value = textBox.Text; + try + { + eTextBox.Value = textBox.Text; + } + catch (Exception ex) + { + DisplayException(ex.Message); + } }; eTextBox.OnValueChanged += () => { @@ -559,7 +580,14 @@ namespace DesktopMagic }; integerUpDown.ValueChanged += (_s, _e) => { - eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault(); + try + { + eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault(); + } + catch (Exception ex) + { + DisplayException(ex.Message); + } }; eIntegerUpDown.OnValueChanged += () => { @@ -584,7 +612,14 @@ namespace DesktopMagic }; slider.ValueChanged += (_s, _e) => { - eSlider.Value = slider.Value; + try + { + eSlider.Value = slider.Value; + } + catch (Exception ex) + { + DisplayException(ex.Message); + } }; eSlider.OnValueChanged += () => { @@ -600,6 +635,16 @@ namespace DesktopMagic } } + private void DisplayException(string message) + { + Logger.Log(message, "PluginInput"); + _ = MessageBox.Show("File execution error:\n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); + int index = WindowNames.IndexOf(((Tuple)optionsComboBox.SelectedItem).Item1.ToString()); + + PluginWindow window = Windows[index] as PluginWindow; + window?.Exit(); + } + #endregion options private void TextBlock_Loaded(object sender, RoutedEventArgs e) diff --git a/src/DesktopMagic/Plugin/PluginWindow.xaml.cs b/src/DesktopMagic/Plugin/PluginWindow.xaml.cs index f7a110c..6a7bc1f 100644 --- a/src/DesktopMagic/Plugin/PluginWindow.xaml.cs +++ b/src/DesktopMagic/Plugin/PluginWindow.xaml.cs @@ -225,25 +225,29 @@ namespace DesktopMagic { try { - Bitmap result = pluginClassInstance.Main(); + if (!stop) + { + Bitmap result = pluginClassInstance.Main(); - if (pluginClassInstance.UpdateInterval > 0) - { - valueTimer.Interval = pluginClassInstance.UpdateInterval; - } - else - { - valueTimer.Stop(); - } + if (pluginClassInstance.UpdateInterval > 0) + { + valueTimer.Interval = pluginClassInstance.UpdateInterval; + } + else + { + valueTimer.Stop(); + } - //Update Image - Dispatcher.Invoke(() => - { - image.Source = BitmapToImageSource(result); - }); + //Update Image + Dispatcher.Invoke(() => + { + image.Source = BitmapToImageSource(result); + }); + } } catch (Exception ex) { + stop = true; MainWindow.Logger.Log(ex.ToString(), "Plugin"); _ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Exit(); @@ -272,8 +276,9 @@ namespace DesktopMagic return bitmapSource; } - private void Exit() + public void Exit() { + stop = true; Dispatcher.Invoke(() => { OnExit?.Invoke(); diff --git a/src/DesktopMagicPlugin.Test/PluginScript.cs b/src/DesktopMagicPlugin.Test/PluginScript.cs index a6f5f15..0f9a18a 100644 --- a/src/DesktopMagicPlugin.Test/PluginScript.cs +++ b/src/DesktopMagicPlugin.Test/PluginScript.cs @@ -2,9 +2,10 @@ using DesktopMagicPluginAPI.Inputs; using System.Drawing; using System.Drawing.Imaging; -using System.Drawing.Drawing2D; using System.IO; using System.Collections.Generic; +using System.Threading.Tasks; +using System; namespace DesktopMagicPlugin.Test { @@ -13,35 +14,58 @@ namespace DesktopMagicPlugin.Test [Element("Gif path:")] private TextBox input = new TextBox(""); + [Element] + private Label info = new Label(""); + private List bitmaps = new List(); private int frameCount = -1; + private const string SaveFilePath = "gifPath.txt"; + public override void Start() { input.OnValueChanged += Input_OnValueChanged; + if (File.Exists(SaveFilePath)) + { + input.Value = File.ReadAllText(SaveFilePath); + } } private void Input_OnValueChanged() { - try + _ = Task.Run(() => { - if (File.Exists(input.Value)) + try { - Image gif = Image.FromFile(input.Value); - PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus - - UpdateInterval = (item.Value[0] + item.Value[1] * 256) * 10; //FrameDelay in ms - bitmaps.Clear(); - for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++) + info.Value = "Loading..."; + if (File.Exists(input.Value)) { - gif.SelectActiveFrame(FrameDimension.Time, i); + Image gif = Image.FromFile(input.Value); - bitmaps.Add(new Bitmap(gif)); + PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus + + UpdateInterval = (item.Value[0] + item.Value[1] * 256) * 10; //FrameDelay in ms + bitmaps.Clear(); + for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++) + { + gif.SelectActiveFrame(FrameDimension.Time, i); + + bitmaps.Add(new Bitmap(gif)); + } + File.WriteAllText(SaveFilePath, input.Value); + info.Value = string.Empty; + } + else + { + info.Value = "File not found!"; } } - } - catch { } + catch (Exception ex) + { + info.Value = $"Error: {ex.Message}"; + } + }); } public override Bitmap Main() diff --git a/src/DesktopMagicPluginAPI/Inputs/Button.cs b/src/DesktopMagicPluginAPI/Inputs/Button.cs index 15c23fb..c8e813c 100644 --- a/src/DesktopMagicPluginAPI/Inputs/Button.cs +++ b/src/DesktopMagicPluginAPI/Inputs/Button.cs @@ -1,5 +1,4 @@ using System; -using System.Threading.Tasks; namespace DesktopMagicPluginAPI.Inputs { @@ -45,7 +44,7 @@ namespace DesktopMagicPluginAPI.Inputs /// public void Click() { - _ = Task.Run(() => OnClick?.Invoke()); + OnClick?.Invoke(); } } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Inputs/Element.cs b/src/DesktopMagicPluginAPI/Inputs/Element.cs index 810cfcf..b23b4fa 100644 --- a/src/DesktopMagicPluginAPI/Inputs/Element.cs +++ b/src/DesktopMagicPluginAPI/Inputs/Element.cs @@ -1,5 +1,4 @@ using System; -using System.Threading.Tasks; namespace DesktopMagicPluginAPI.Inputs { @@ -18,7 +17,7 @@ namespace DesktopMagicPluginAPI.Inputs /// protected void ValueChanged() { - _ = Task.Run(() => OnValueChanged?.Invoke()); + OnValueChanged?.Invoke(); } } } \ No newline at end of file