- 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
+50 -5
View File
@@ -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<string, int>)optionsComboBox.SelectedItem).Item1.ToString());
PluginWindow window = Windows[index] as PluginWindow;
window?.Exit();
}
#endregion options
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
+20 -15
View File
@@ -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();
+37 -13
View File
@@ -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<Bitmap> bitmaps = new List<Bitmap>();
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()
+1 -2
View File
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
namespace DesktopMagicPluginAPI.Inputs
{
@@ -45,7 +44,7 @@ namespace DesktopMagicPluginAPI.Inputs
/// </summary>
public void Click()
{
_ = Task.Run(() => OnClick?.Invoke());
OnClick?.Invoke();
}
}
}
+1 -2
View File
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
namespace DesktopMagicPluginAPI.Inputs
{
@@ -18,7 +17,7 @@ namespace DesktopMagicPluginAPI.Inputs
/// </summary>
protected void ValueChanged()
{
_ = Task.Run(() => OnValueChanged?.Invoke());
OnValueChanged?.Invoke();
}
}
}