From 2ae415f00faaa00efc21f6b691064398154c9d07 Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Sun, 17 May 2020 19:57:07 +0800 Subject: [PATCH] optimize tasks --- Pixiview/Illust/IllustCollectionPage.cs | 23 +++++++++++-- Pixiview/Illust/UserIllustPage.xaml.cs | 7 ---- Pixiview/Illust/ViewIllustPage.xaml.cs | 18 +++++++++-- Pixiview/UI/AdaptedPage.cs | 2 +- Pixiview/Utils/Extensions.cs | 40 ++++++++++++++++++----- Pixiview/Utils/Stores.cs | 43 +++++++++---------------- 6 files changed, 85 insertions(+), 48 deletions(-) diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs index e1651c0..62e2fa7 100644 --- a/Pixiview/Illust/IllustCollectionPage.cs +++ b/Pixiview/Illust/IllustCollectionPage.cs @@ -75,6 +75,7 @@ namespace Pixiview.Illust protected double topOffset; private T illustData; + private ParallelTask task; public IllustCollectionPage() { @@ -95,6 +96,11 @@ namespace Pixiview.Illust public override void OnUnload() { + if (task != null) + { + task.Dispose(); + task = null; + } InvalidateCollection(); Illusts = null; lastUpdated = default; @@ -559,7 +565,12 @@ namespace Pixiview.Illust 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) { @@ -716,11 +727,17 @@ namespace Pixiview.Illust } private readonly object sync = new object(); - private volatile bool running; + private bool running; public bool Running { - get => running; + get + { + lock (sync) + { + return running; + } + } set { lock (sync) diff --git a/Pixiview/Illust/UserIllustPage.xaml.cs b/Pixiview/Illust/UserIllustPage.xaml.cs index 888e7b0..e1b3f53 100644 --- a/Pixiview/Illust/UserIllustPage.xaml.cs +++ b/Pixiview/Illust/UserIllustPage.xaml.cs @@ -109,13 +109,6 @@ namespace Pixiview.Illust return; } await ScrollToTopAsync(scrollView); - // release - var collection = IllustCollection; - if (collection != null) - { - collection.Running = false; - IllustCollection = null; - } startIndex = -1; nextIndex = 0; illustIds = null; diff --git a/Pixiview/Illust/ViewIllustPage.xaml.cs b/Pixiview/Illust/ViewIllustPage.xaml.cs index 047bc53..6e6f355 100644 --- a/Pixiview/Illust/ViewIllustPage.xaml.cs +++ b/Pixiview/Illust/ViewIllustPage.xaml.cs @@ -128,6 +128,7 @@ namespace Pixiview.Illust private readonly ImageSource fontIconNotLove; private IllustUgoiraData ugoiraData; private Ugoira ugoira; + private ParallelTask task; private readonly object sync = new object(); private int downloaded = 0; @@ -188,6 +189,15 @@ namespace Pixiview.Illust } } + public override void OnUnload() + { + if (task != null) + { + task.Dispose(); + task = null; + } + } + #if __IOS__ 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); return true; @@ -461,7 +476,6 @@ namespace Pixiview.Illust else { var val = downloaded / (float)pageCount; - App.DebugPrint($"download progress: {val}"); MainThread.BeginInvokeOnMainThread(() => { ViewExtensions.CancelAnimations(progress); diff --git a/Pixiview/UI/AdaptedPage.cs b/Pixiview/UI/AdaptedPage.cs index 055b2bd..2dab670 100644 --- a/Pixiview/UI/AdaptedPage.cs +++ b/Pixiview/UI/AdaptedPage.cs @@ -153,7 +153,7 @@ namespace Pixiview.UI return _instance; } - private volatile bool isBusy = false; + private bool isBusy = false; public void Dispose() { diff --git a/Pixiview/Utils/Extensions.cs b/Pixiview/Utils/Extensions.cs index 9d9b017..903a265 100644 --- a/Pixiview/Utils/Extensions.cs +++ b/Pixiview/Utils/Extensions.cs @@ -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 action) + public static ParallelTask Start(int from, int toExclusive, int maxCount, Predicate action) { var task = new ParallelTask(from, toExclusive, maxCount, action); Task.Run(task.Start); + return task; } - private volatile int count; - private volatile bool disposed; + private readonly object sync = new object(); + private int count; + private bool disposed; private readonly int max; private readonly int from; @@ -130,7 +132,12 @@ namespace Pixiview.Utils for (int i = from; i < to; i++) { var index = i; - while (count >= max) + bool flag; + lock (sync) + { + flag = count >= max; + } + while (flag) { if (disposed) { @@ -139,14 +146,20 @@ namespace Pixiview.Utils } Thread.Sleep(100); } - count++; + lock (sync) + { + count++; + } Task.Run(() => { try { if (!action(index)) { - disposed = true; + lock (sync) + { + disposed = true; + } } } catch (Exception ex) @@ -155,11 +168,22 @@ namespace Pixiview.Utils } finally { - count--; + lock (sync) + { + count--; + } } }); } } + + public void Dispose() + { + lock (sync) + { + disposed = true; + } + } } public static class Screen diff --git a/Pixiview/Utils/Stores.cs b/Pixiview/Utils/Stores.cs index 47caeb0..3eeb2b1 100644 --- a/Pixiview/Utils/Stores.cs +++ b/Pixiview/Utils/Stores.cs @@ -32,8 +32,6 @@ namespace Pixiview.Utils private const string userFolder = "user-profile"; private const string recommendsFolder = "recommends"; - private static readonly object sync = new object(); - public static bool NetworkAvailable { get @@ -57,25 +55,22 @@ namespace Pixiview.Utils public static IllustFavorite GetFavoriteObject(bool force = false) { - lock (sync) + if (force || favoriteObject == null) { - if (force || favoriteObject == null) + var favorites = LoadFavoritesIllusts(); + if (favorites != null) { - var favorites = LoadFavoritesIllusts(); - if (favorites != null) - { - favoriteObject = favorites; - } - else - { - favoriteObject = new IllustFavorite - { - Illusts = new FavoriteList() - }; - } + favoriteObject = favorites; + } + else + { + favoriteObject = new IllustFavorite + { + Illusts = new FavoriteList() + }; } - return favoriteObject; } + return favoriteObject; } public static IllustFavorite LoadFavoritesIllusts(string file = null) @@ -84,21 +79,15 @@ namespace Pixiview.Utils { file = FavoritesPath; } - lock (sync) - { - return ReadObject(file); - } + return ReadObject(file); } public static void SaveFavoritesIllusts() { var file = FavoritesPath; - lock (sync) - { - var data = GetFavoriteObject(); - data.LastFavoriteUtc = DateTime.UtcNow; - WriteObject(file, data); - } + var data = GetFavoriteObject(); + data.LastFavoriteUtc = DateTime.UtcNow; + WriteObject(file, data); } public static string LoadUgoiraImage(string zip, string frame)