make ugoira disposable

This commit is contained in:
Tsanie Lily 2020-05-15 09:18:02 +08:00
parent da8da3be4d
commit 2fe66d4f8d
3 changed files with 49 additions and 18 deletions

View File

@ -504,7 +504,7 @@ namespace Pixiview.Illust
public abstract class IllustScrollableCollectionPage<T> : IllustCollectionPage<T> public abstract class IllustScrollableCollectionPage<T> : IllustCollectionPage<T>
{ {
protected const int SCROLL_OFFSET = 33; protected const int SCROLL_OFFSET = 33;
protected static readonly object sync = new object(); protected readonly object sync = new object();
private double lastScrollY = double.MinValue; private double lastScrollY = double.MinValue;
private double offset; private double offset;

View File

@ -153,8 +153,10 @@ namespace Pixiview.Illust
if (ugoira != null) if (ugoira != null)
{ {
ugoira.TogglePlay(false);
IllustItem.IsPlaying = false; IllustItem.IsPlaying = false;
ugoira.TogglePlay(false);
ugoira.Dispose();
ugoira = null;
} }
if (saveFavorites) if (saveFavorites)

View File

@ -292,8 +292,10 @@ namespace Pixiview.Utils
} }
} }
public class Ugoira public class Ugoira : IDisposable
{ {
private readonly object sync = new object();
private readonly IllustUgoiraBody ugoira; private readonly IllustUgoiraBody ugoira;
private readonly IllustDetailItem detailItem; private readonly IllustDetailItem detailItem;
private readonly ImageSource[] frames; private readonly ImageSource[] frames;
@ -315,21 +317,39 @@ namespace Pixiview.Utils
Task.Run(LoadFrames); Task.Run(LoadFrames);
} }
public void Dispose()
{
lock (sync)
{
if (IsPlaying)
{
TogglePlay(false);
}
}
if (timer != null)
{
timer.Dispose();
}
}
public void TogglePlay(bool flag) public void TogglePlay(bool flag)
{ {
if (IsPlaying == flag) lock (sync)
{ {
return; if (IsPlaying == flag)
} {
if (flag) return;
{ }
IsPlaying = true; if (flag)
timer.Change(0, Timeout.Infinite); {
} IsPlaying = true;
else timer.Change(0, Timeout.Infinite);
{ }
IsPlaying = false; else
timer.Change(Timeout.Infinite, Timeout.Infinite); {
IsPlaying = false;
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
} }
} }
@ -359,9 +379,12 @@ namespace Pixiview.Utils
private void OnTimerCallback(object state) private void OnTimerCallback(object state)
{ {
if (!IsPlaying) lock (sync)
{ {
return; if (!IsPlaying)
{
return;
}
} }
ImageSource frame; ImageSource frame;
@ -384,7 +407,13 @@ namespace Pixiview.Utils
i = 0; i = 0;
} }
index = i; index = i;
timer.Change(info.delay, Timeout.Infinite); lock (sync)
{
if (IsPlaying)
{
timer.Change(info.delay, Timeout.Infinite);
}
}
} }
private const int BUFFER_SIZE = 300000; private const int BUFFER_SIZE = 300000;