optimize tasks
This commit is contained in:
@ -75,6 +75,7 @@ namespace Pixiview.Illust
|
|||||||
protected double topOffset;
|
protected double topOffset;
|
||||||
|
|
||||||
private T illustData;
|
private T illustData;
|
||||||
|
private ParallelTask task;
|
||||||
|
|
||||||
public IllustCollectionPage()
|
public IllustCollectionPage()
|
||||||
{
|
{
|
||||||
@ -95,6 +96,11 @@ namespace Pixiview.Illust
|
|||||||
|
|
||||||
public override void OnUnload()
|
public override void OnUnload()
|
||||||
{
|
{
|
||||||
|
if (task != null)
|
||||||
|
{
|
||||||
|
task.Dispose();
|
||||||
|
task = null;
|
||||||
|
}
|
||||||
InvalidateCollection();
|
InvalidateCollection();
|
||||||
Illusts = null;
|
Illusts = null;
|
||||||
lastUpdated = default;
|
lastUpdated = default;
|
||||||
@ -559,7 +565,12 @@ namespace Pixiview.Illust
|
|||||||
|
|
||||||
void DoLoadImages(IllustCollection collection)
|
void DoLoadImages(IllustCollection collection)
|
||||||
{
|
{
|
||||||
ParallelTask.Start(0, collection.Count, Configs.MaxThreads, i =>
|
if (task != null)
|
||||||
|
{
|
||||||
|
task.Dispose();
|
||||||
|
task = null;
|
||||||
|
}
|
||||||
|
task = ParallelTask.Start(0, collection.Count, Configs.MaxThreads, i =>
|
||||||
{
|
{
|
||||||
if (!collection.Running)
|
if (!collection.Running)
|
||||||
{
|
{
|
||||||
@ -716,11 +727,17 @@ namespace Pixiview.Illust
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly object sync = new object();
|
private readonly object sync = new object();
|
||||||
private volatile bool running;
|
private bool running;
|
||||||
|
|
||||||
public bool Running
|
public bool Running
|
||||||
{
|
{
|
||||||
get => running;
|
get
|
||||||
|
{
|
||||||
|
lock (sync)
|
||||||
|
{
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
lock (sync)
|
lock (sync)
|
||||||
|
@ -109,13 +109,6 @@ namespace Pixiview.Illust
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await ScrollToTopAsync(scrollView);
|
await ScrollToTopAsync(scrollView);
|
||||||
// release
|
|
||||||
var collection = IllustCollection;
|
|
||||||
if (collection != null)
|
|
||||||
{
|
|
||||||
collection.Running = false;
|
|
||||||
IllustCollection = null;
|
|
||||||
}
|
|
||||||
startIndex = -1;
|
startIndex = -1;
|
||||||
nextIndex = 0;
|
nextIndex = 0;
|
||||||
illustIds = null;
|
illustIds = null;
|
||||||
|
@ -128,6 +128,7 @@ namespace Pixiview.Illust
|
|||||||
private readonly ImageSource fontIconNotLove;
|
private readonly ImageSource fontIconNotLove;
|
||||||
private IllustUgoiraData ugoiraData;
|
private IllustUgoiraData ugoiraData;
|
||||||
private Ugoira ugoira;
|
private Ugoira ugoira;
|
||||||
|
private ParallelTask task;
|
||||||
|
|
||||||
private readonly object sync = new object();
|
private readonly object sync = new object();
|
||||||
private int downloaded = 0;
|
private int downloaded = 0;
|
||||||
@ -188,6 +189,15 @@ namespace Pixiview.Illust
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void OnUnload()
|
||||||
|
{
|
||||||
|
if (task != null)
|
||||||
|
{
|
||||||
|
task.Dispose();
|
||||||
|
task = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if __IOS__
|
#if __IOS__
|
||||||
protected override void OnPageTopMarginChanged(Thickness old, Thickness @new)
|
protected override void OnPageTopMarginChanged(Thickness old, Thickness @new)
|
||||||
{
|
{
|
||||||
@ -387,7 +397,12 @@ namespace Pixiview.Illust
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ParallelTask.Start(0, items.Length, Configs.MaxPageThreads, i =>
|
if (task != null)
|
||||||
|
{
|
||||||
|
task.Dispose();
|
||||||
|
task = null;
|
||||||
|
}
|
||||||
|
task = ParallelTask.Start(0, items.Length, Configs.MaxPageThreads, i =>
|
||||||
{
|
{
|
||||||
DoLoadImage(i);
|
DoLoadImage(i);
|
||||||
return true;
|
return true;
|
||||||
@ -461,7 +476,6 @@ namespace Pixiview.Illust
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var val = downloaded / (float)pageCount;
|
var val = downloaded / (float)pageCount;
|
||||||
App.DebugPrint($"download progress: {val}");
|
|
||||||
MainThread.BeginInvokeOnMainThread(() =>
|
MainThread.BeginInvokeOnMainThread(() =>
|
||||||
{
|
{
|
||||||
ViewExtensions.CancelAnimations(progress);
|
ViewExtensions.CancelAnimations(progress);
|
||||||
|
@ -153,7 +153,7 @@ namespace Pixiview.UI
|
|||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private volatile bool isBusy = false;
|
private bool isBusy = false;
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
@ -93,16 +93,18 @@ namespace Pixiview.Utils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ParallelTask
|
public class ParallelTask : IDisposable
|
||||||
{
|
{
|
||||||
public static void Start(int from, int toExclusive, int maxCount, Predicate<int> action)
|
public static ParallelTask Start(int from, int toExclusive, int maxCount, Predicate<int> action)
|
||||||
{
|
{
|
||||||
var task = new ParallelTask(from, toExclusive, maxCount, action);
|
var task = new ParallelTask(from, toExclusive, maxCount, action);
|
||||||
Task.Run(task.Start);
|
Task.Run(task.Start);
|
||||||
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
private volatile int count;
|
private readonly object sync = new object();
|
||||||
private volatile bool disposed;
|
private int count;
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
private readonly int max;
|
private readonly int max;
|
||||||
private readonly int from;
|
private readonly int from;
|
||||||
@ -130,7 +132,12 @@ namespace Pixiview.Utils
|
|||||||
for (int i = from; i < to; i++)
|
for (int i = from; i < to; i++)
|
||||||
{
|
{
|
||||||
var index = i;
|
var index = i;
|
||||||
while (count >= max)
|
bool flag;
|
||||||
|
lock (sync)
|
||||||
|
{
|
||||||
|
flag = count >= max;
|
||||||
|
}
|
||||||
|
while (flag)
|
||||||
{
|
{
|
||||||
if (disposed)
|
if (disposed)
|
||||||
{
|
{
|
||||||
@ -139,27 +146,44 @@ namespace Pixiview.Utils
|
|||||||
}
|
}
|
||||||
Thread.Sleep(100);
|
Thread.Sleep(100);
|
||||||
}
|
}
|
||||||
|
lock (sync)
|
||||||
|
{
|
||||||
count++;
|
count++;
|
||||||
|
}
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!action(index))
|
if (!action(index))
|
||||||
|
{
|
||||||
|
lock (sync)
|
||||||
{
|
{
|
||||||
disposed = true;
|
disposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
App.DebugError("parallel.start", $"failed to run action, index: {i}, error: {ex.Message}");
|
App.DebugError("parallel.start", $"failed to run action, index: {i}, error: {ex.Message}");
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
{
|
||||||
|
lock (sync)
|
||||||
{
|
{
|
||||||
count--;
|
count--;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
lock (sync)
|
||||||
|
{
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Screen
|
public static class Screen
|
||||||
|
@ -32,8 +32,6 @@ namespace Pixiview.Utils
|
|||||||
private const string userFolder = "user-profile";
|
private const string userFolder = "user-profile";
|
||||||
private const string recommendsFolder = "recommends";
|
private const string recommendsFolder = "recommends";
|
||||||
|
|
||||||
private static readonly object sync = new object();
|
|
||||||
|
|
||||||
public static bool NetworkAvailable
|
public static bool NetworkAvailable
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -56,8 +54,6 @@ namespace Pixiview.Utils
|
|||||||
private static IllustFavorite favoriteObject;
|
private static IllustFavorite favoriteObject;
|
||||||
|
|
||||||
public static IllustFavorite GetFavoriteObject(bool force = false)
|
public static IllustFavorite GetFavoriteObject(bool force = false)
|
||||||
{
|
|
||||||
lock (sync)
|
|
||||||
{
|
{
|
||||||
if (force || favoriteObject == null)
|
if (force || favoriteObject == null)
|
||||||
{
|
{
|
||||||
@ -76,7 +72,6 @@ namespace Pixiview.Utils
|
|||||||
}
|
}
|
||||||
return favoriteObject;
|
return favoriteObject;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static IllustFavorite LoadFavoritesIllusts(string file = null)
|
public static IllustFavorite LoadFavoritesIllusts(string file = null)
|
||||||
{
|
{
|
||||||
@ -84,22 +79,16 @@ namespace Pixiview.Utils
|
|||||||
{
|
{
|
||||||
file = FavoritesPath;
|
file = FavoritesPath;
|
||||||
}
|
}
|
||||||
lock (sync)
|
|
||||||
{
|
|
||||||
return ReadObject<IllustFavorite>(file);
|
return ReadObject<IllustFavorite>(file);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void SaveFavoritesIllusts()
|
public static void SaveFavoritesIllusts()
|
||||||
{
|
{
|
||||||
var file = FavoritesPath;
|
var file = FavoritesPath;
|
||||||
lock (sync)
|
|
||||||
{
|
|
||||||
var data = GetFavoriteObject();
|
var data = GetFavoriteObject();
|
||||||
data.LastFavoriteUtc = DateTime.UtcNow;
|
data.LastFavoriteUtc = DateTime.UtcNow;
|
||||||
WriteObject(file, data);
|
WriteObject(file, data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static string LoadUgoiraImage(string zip, string frame)
|
public static string LoadUgoiraImage(string zip, string frame)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user