- Exception handling for plugin events

- Revert: Execute `OnValueChanged` event in task
This commit is contained in:
Stone-Red-Code
2021-09-14 14:07:08 +02:00
parent e00c873bd5
commit 6e4757adea
5 changed files with 109 additions and 37 deletions
+45
View File
@@ -489,8 +489,15 @@ namespace DesktopMagic
HorizontalAlignment = HorizontalAlignment.Stretch HorizontalAlignment = HorizontalAlignment.Stretch
}; };
button.Click += (_s, _e) => button.Click += (_s, _e) =>
{
try
{ {
eButton.Click(); eButton.Click();
}
catch (Exception ex)
{
DisplayException(ex.Message);
}
}; };
eButton.OnValueChanged += () => eButton.OnValueChanged += () =>
{ {
@@ -512,8 +519,15 @@ namespace DesktopMagic
HorizontalAlignment = HorizontalAlignment.Stretch HorizontalAlignment = HorizontalAlignment.Stretch
}; };
checkBox.Click += (_s, _e) => checkBox.Click += (_s, _e) =>
{
try
{ {
eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault(); eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault();
}
catch (Exception ex)
{
DisplayException(ex.Message);
}
}; };
eCheckBox.OnValueChanged += () => eCheckBox.OnValueChanged += () =>
{ {
@@ -535,8 +549,15 @@ namespace DesktopMagic
HorizontalAlignment = HorizontalAlignment.Stretch HorizontalAlignment = HorizontalAlignment.Stretch
}; };
textBox.TextChanged += (_s, _e) => textBox.TextChanged += (_s, _e) =>
{
try
{ {
eTextBox.Value = textBox.Text; eTextBox.Value = textBox.Text;
}
catch (Exception ex)
{
DisplayException(ex.Message);
}
}; };
eTextBox.OnValueChanged += () => eTextBox.OnValueChanged += () =>
{ {
@@ -558,8 +579,15 @@ namespace DesktopMagic
HorizontalAlignment = HorizontalAlignment.Stretch HorizontalAlignment = HorizontalAlignment.Stretch
}; };
integerUpDown.ValueChanged += (_s, _e) => integerUpDown.ValueChanged += (_s, _e) =>
{
try
{ {
eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault(); eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault();
}
catch (Exception ex)
{
DisplayException(ex.Message);
}
}; };
eIntegerUpDown.OnValueChanged += () => eIntegerUpDown.OnValueChanged += () =>
{ {
@@ -583,8 +611,15 @@ namespace DesktopMagic
HorizontalAlignment = HorizontalAlignment.Stretch HorizontalAlignment = HorizontalAlignment.Stretch
}; };
slider.ValueChanged += (_s, _e) => slider.ValueChanged += (_s, _e) =>
{
try
{ {
eSlider.Value = slider.Value; eSlider.Value = slider.Value;
}
catch (Exception ex)
{
DisplayException(ex.Message);
}
}; };
eSlider.OnValueChanged += () => 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<string, int>)optionsComboBox.SelectedItem).Item1.ToString());
PluginWindow window = Windows[index] as PluginWindow;
window?.Exit();
}
#endregion options #endregion options
private void TextBlock_Loaded(object sender, RoutedEventArgs e) private void TextBlock_Loaded(object sender, RoutedEventArgs e)
+6 -1
View File
@@ -224,6 +224,8 @@ namespace DesktopMagic
private void ValueTimer_Elapsed(object sender, ElapsedEventArgs e) private void ValueTimer_Elapsed(object sender, ElapsedEventArgs e)
{ {
try try
{
if (!stop)
{ {
Bitmap result = pluginClassInstance.Main(); Bitmap result = pluginClassInstance.Main();
@@ -242,8 +244,10 @@ namespace DesktopMagic
image.Source = BitmapToImageSource(result); image.Source = BitmapToImageSource(result);
}); });
} }
}
catch (Exception ex) catch (Exception ex)
{ {
stop = true;
MainWindow.Logger.Log(ex.ToString(), "Plugin"); MainWindow.Logger.Log(ex.ToString(), "Plugin");
_ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); _ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Exit(); Exit();
@@ -272,8 +276,9 @@ namespace DesktopMagic
return bitmapSource; return bitmapSource;
} }
private void Exit() public void Exit()
{ {
stop = true;
Dispatcher.Invoke(() => Dispatcher.Invoke(() =>
{ {
OnExit?.Invoke(); OnExit?.Invoke();
+26 -2
View File
@@ -2,9 +2,10 @@
using DesktopMagicPluginAPI.Inputs; using DesktopMagicPluginAPI.Inputs;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using System;
namespace DesktopMagicPlugin.Test namespace DesktopMagicPlugin.Test
{ {
@@ -13,22 +14,35 @@ namespace DesktopMagicPlugin.Test
[Element("Gif path:")] [Element("Gif path:")]
private TextBox input = new TextBox(""); private TextBox input = new TextBox("");
[Element]
private Label info = new Label("");
private List<Bitmap> bitmaps = new List<Bitmap>(); private List<Bitmap> bitmaps = new List<Bitmap>();
private int frameCount = -1; private int frameCount = -1;
private const string SaveFilePath = "gifPath.txt";
public override void Start() public override void Start()
{ {
input.OnValueChanged += Input_OnValueChanged; input.OnValueChanged += Input_OnValueChanged;
if (File.Exists(SaveFilePath))
{
input.Value = File.ReadAllText(SaveFilePath);
}
} }
private void Input_OnValueChanged() private void Input_OnValueChanged()
{
_ = Task.Run(() =>
{ {
try try
{ {
info.Value = "Loading...";
if (File.Exists(input.Value)) if (File.Exists(input.Value))
{ {
Image gif = Image.FromFile(input.Value); Image gif = Image.FromFile(input.Value);
PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus
UpdateInterval = (item.Value[0] + item.Value[1] * 256) * 10; //FrameDelay in ms UpdateInterval = (item.Value[0] + item.Value[1] * 256) * 10; //FrameDelay in ms
@@ -39,9 +53,19 @@ namespace DesktopMagicPlugin.Test
bitmaps.Add(new Bitmap(gif)); 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() public override Bitmap Main()
+1 -2
View File
@@ -1,5 +1,4 @@
using System; using System;
using System.Threading.Tasks;
namespace DesktopMagicPluginAPI.Inputs namespace DesktopMagicPluginAPI.Inputs
{ {
@@ -45,7 +44,7 @@ namespace DesktopMagicPluginAPI.Inputs
/// </summary> /// </summary>
public void Click() public void Click()
{ {
_ = Task.Run(() => OnClick?.Invoke()); OnClick?.Invoke();
} }
} }
} }
+1 -2
View File
@@ -1,5 +1,4 @@
using System; using System;
using System.Threading.Tasks;
namespace DesktopMagicPluginAPI.Inputs namespace DesktopMagicPluginAPI.Inputs
{ {
@@ -18,7 +17,7 @@ namespace DesktopMagicPluginAPI.Inputs
/// </summary> /// </summary>
protected void ValueChanged() protected void ValueChanged()
{ {
_ = Task.Run(() => OnValueChanged?.Invoke()); OnValueChanged?.Invoke();
} }
} }
} }