- 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
+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()