mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 08:56:15 +02:00
Update test plugin to properly release the resources when being stopped
This commit is contained in:
@@ -6,14 +6,13 @@ using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DesktopMagic.PluginTest;
|
||||
|
||||
public class GifPlugin : Plugin
|
||||
{
|
||||
private const string SaveFilePath = "gifPath.txt";
|
||||
|
||||
[Setting("gif-path", "Gif path:")]
|
||||
private readonly TextBox input = new TextBox("");
|
||||
|
||||
@@ -22,22 +21,33 @@ public class GifPlugin : Plugin
|
||||
|
||||
private readonly List<Bitmap> bitmaps = [];
|
||||
|
||||
private readonly CancellationTokenSource cancellationTokenSource;
|
||||
private int frameCount = -1;
|
||||
|
||||
public override void Start()
|
||||
public GifPlugin()
|
||||
{
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
public override async void Start()
|
||||
{
|
||||
input.OnValueChanged += Input_OnValueChanged;
|
||||
if (File.Exists(SaveFilePath))
|
||||
{
|
||||
input.Value = File.ReadAllText(SaveFilePath);
|
||||
}
|
||||
|
||||
await LoadGif();
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
cancellationTokenSource.Cancel();
|
||||
|
||||
bitmaps.Clear();
|
||||
}
|
||||
|
||||
public override Bitmap Main()
|
||||
{
|
||||
if (bitmaps.Count == 0)
|
||||
{
|
||||
return new Bitmap(1, 1);
|
||||
return null;
|
||||
}
|
||||
|
||||
frameCount++;
|
||||
@@ -50,9 +60,14 @@ public class GifPlugin : Plugin
|
||||
return bitmaps[frameCount];
|
||||
}
|
||||
|
||||
private void Input_OnValueChanged()
|
||||
private async void Input_OnValueChanged()
|
||||
{
|
||||
_ = Task.Run(() =>
|
||||
await LoadGif();
|
||||
}
|
||||
|
||||
private Task LoadGif()
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -63,15 +78,19 @@ public class GifPlugin : Plugin
|
||||
|
||||
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();
|
||||
for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++)
|
||||
{
|
||||
cancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
|
||||
_ = gif.SelectActiveFrame(FrameDimension.Time, i);
|
||||
|
||||
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;
|
||||
}
|
||||
else
|
||||
@@ -83,6 +102,6 @@ public class GifPlugin : Plugin
|
||||
{
|
||||
info.Value = $"Error: {ex.Message}";
|
||||
}
|
||||
});
|
||||
}, cancellationTokenSource.Token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user