Update test plugin to properly release the resources when being stopped

This commit is contained in:
Stone_Red
2023-11-22 16:02:31 +01:00
parent 69672da48f
commit bb560cd9db
+32 -13
View File
@@ -6,14 +6,13 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DesktopMagic.PluginTest; namespace DesktopMagic.PluginTest;
public class GifPlugin : Plugin public class GifPlugin : Plugin
{ {
private const string SaveFilePath = "gifPath.txt";
[Setting("gif-path", "Gif path:")] [Setting("gif-path", "Gif path:")]
private readonly TextBox input = new TextBox(""); private readonly TextBox input = new TextBox("");
@@ -22,22 +21,33 @@ public class GifPlugin : Plugin
private readonly List<Bitmap> bitmaps = []; private readonly List<Bitmap> bitmaps = [];
private readonly CancellationTokenSource cancellationTokenSource;
private int frameCount = -1; private int frameCount = -1;
public override void Start() public GifPlugin()
{
cancellationTokenSource = new CancellationTokenSource();
}
public override async void Start()
{ {
input.OnValueChanged += Input_OnValueChanged; input.OnValueChanged += Input_OnValueChanged;
if (File.Exists(SaveFilePath))
{ await LoadGif();
input.Value = File.ReadAllText(SaveFilePath); }
}
public override void Stop()
{
cancellationTokenSource.Cancel();
bitmaps.Clear();
} }
public override Bitmap Main() public override Bitmap Main()
{ {
if (bitmaps.Count == 0) if (bitmaps.Count == 0)
{ {
return new Bitmap(1, 1); return null;
} }
frameCount++; frameCount++;
@@ -50,9 +60,14 @@ public class GifPlugin : Plugin
return bitmaps[frameCount]; return bitmaps[frameCount];
} }
private void Input_OnValueChanged() private async void Input_OnValueChanged()
{ {
_ = Task.Run(() => await LoadGif();
}
private Task LoadGif()
{
return Task.Run(() =>
{ {
try try
{ {
@@ -63,15 +78,19 @@ public class GifPlugin : Plugin
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
bitmaps.Clear(); bitmaps.Clear();
for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++) for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++)
{ {
cancellationTokenSource.Token.ThrowIfCancellationRequested();
_ = gif.SelectActiveFrame(FrameDimension.Time, i); _ = gif.SelectActiveFrame(FrameDimension.Time, i);
bitmaps.Add(new Bitmap(gif)); bitmaps.Add(new Bitmap(gif));
info.Value = $"Loading frame {i + 1} of {gif.GetFrameCount(FrameDimension.Time)}";
} }
File.WriteAllText(SaveFilePath, input.Value);
info.Value = string.Empty; info.Value = string.Empty;
} }
else else
@@ -83,6 +102,6 @@ public class GifPlugin : Plugin
{ {
info.Value = $"Error: {ex.Message}"; info.Value = $"Error: {ex.Message}";
} }
}); }, cancellationTokenSource.Token);
} }
} }