public static IAsyncActionWithProgress <double> ClearCachedGalleriesAsync() { return(Run <double>(async(token, progress) => { using (var db = new GalleryDb()) { var folder = GalleryImage.ImageFolder ?? await GalleryImage.GetImageFolderAsync(); var todelete = await db.ImageSet .Where(im => !db.SavedSet.Any(sm => im.UsingBy.Any(gi => gi.GalleryId == sm.GalleryId))) .ToListAsync(token); double count = todelete.Count; var i = 0; foreach (var item in todelete) { var file = await folder.TryGetFileAsync(item.FileName); if (file != null) { await file.DeleteAsync(); } progress.Report(++i / count); } db.ImageSet.RemoveRange(todelete); await db.SaveChangesAsync(); } })); }
public virtual IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>(async(token, progress) => { if (this.HasMoreItems) { progress.Report(new SaveGalleryProgress(-1, this.RecordCount)); while (this.HasMoreItems) { await this.LoadMoreItemsAsync((uint)PageSize); token.ThrowIfCancellationRequested(); } } await Task.Delay(0).ConfigureAwait(false); var loadedCount = 0; progress.Report(new SaveGalleryProgress(loadedCount, this.RecordCount)); using (var semaphore = new SemaphoreSlim(16, 16)) { var loadTasks = this.Select(image => Task.Run(async() => { await semaphore.WaitAsync(); try { token.ThrowIfCancellationRequested(); await image.LoadImageAsync(false, strategy, true); Interlocked.Increment(ref loadedCount); progress.Report(new SaveGalleryProgress(loadedCount, this.RecordCount)); } finally { semaphore.Release(); } })).ToArray(); await Task.WhenAll(loadTasks).ConfigureAwait(false); } using (var db = new GalleryDb()) { var gid = this.ID; var myModel = db.SavedSet.SingleOrDefault(model => model.GalleryId == gid); if (myModel == null) { db.SavedSet.Add(new SavedGalleryModel().Update(this)); } else { myModel.Update(this); } await db.SaveChangesAsync(); } })); }
public override IAsyncAction DeleteAsync() { return(Task.Run(async() => { using (var db = new GalleryDb()) { var gid = this.ID; db.SavedSet.Remove(db.SavedSet.Single(c => c.GalleryId == gid)); await db.SaveChangesAsync(); } await base.DeleteAsync(); }).AsAsyncAction()); }
public static IAsyncActionWithProgress <double> ClearAllGalleriesAsync() { return(Run <double>(async(token, progress) => { progress.Report(double.NaN); var getFiles = StorageHelper.ImageFolder.GetFilesAsync(); using (var db = new GalleryDb()) { db.SavedSet.RemoveRange(db.SavedSet); db.ImageSet.RemoveRange(db.ImageSet); await db.SaveChangesAsync(); } var files = await getFiles; double c = files.Count; var i = 0; foreach (var item in files) { await item.DeleteAsync(); progress.Report(++i / c); } })); }
public virtual IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>((token, progress) => Task.Run(async() => { await Task.Yield(); progress.Report(new SaveGalleryProgress(-1, Count)); var loadOP = LoadItemsAsync(0, Count); token.Register(loadOP.Cancel); await loadOP; token.ThrowIfCancellationRequested(); var loadedCount = 0; progress.Report(new SaveGalleryProgress(loadedCount, Count)); using (var semaphore = new SemaphoreSlim(10, 10)) { async Task loadSingleImage(GalleryImage i) { try { Debug.WriteLine($"Start {i.PageId}"); token.ThrowIfCancellationRequested(); var firstFailed = false; try { var firstChance = i.LoadImageAsync(false, strategy, true); var firstTask = firstChance.AsTask(token); var c = await Task.WhenAny(Task.Delay(30_000), firstTask); if (c != firstTask) { Debug.WriteLine($"Timeout 1st {i.PageId}"); firstFailed = true; firstChance.Cancel(); } } catch (Exception) { Debug.WriteLine($"Fail 1st {i.PageId}"); firstFailed = true; } if (firstFailed) { Debug.WriteLine($"Retry {i.PageId}"); token.ThrowIfCancellationRequested(); await i.LoadImageAsync(true, strategy, true).AsTask(token); } progress.Report(new SaveGalleryProgress(Interlocked.Increment(ref loadedCount), Count)); Debug.WriteLine($"Success {i.PageId}"); } finally { semaphore.Release(); Debug.WriteLine($"End {i.PageId}"); } } var pendingTasks = new List <Task>(Count); await Task.Run(async() => { foreach (var item in this) { await semaphore.WaitAsync().ConfigureAwait(false); pendingTasks.Add(loadSingleImage(item)); } }, token).ConfigureAwait(false); await Task.WhenAll(pendingTasks).ConfigureAwait(false); } using (var db = new GalleryDb()) { var gid = Id; var myModel = db.SavedSet.SingleOrDefault(model => model.GalleryId == gid); if (myModel is null) { db.SavedSet.Add(new SavedGalleryModel().Update(this)); } else { myModel.Update(this); } await db.SaveChangesAsync(); } }))); }
public virtual IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>((token, progress) => Task.Run(async() => { await Task.Yield(); progress.Report(new SaveGalleryProgress(-1, this.Count)); var loadOP = LoadItemsAsync(0, this.Count); token.Register(loadOP.Cancel); await loadOP; token.ThrowIfCancellationRequested(); var loadedCount = 0; progress.Report(new SaveGalleryProgress(loadedCount, this.Count)); using (var semaphore = new SemaphoreSlim(16, 16)) { var loadTasks = this.Select(image => Task.Run(async() => { await semaphore.WaitAsync(); try { token.ThrowIfCancellationRequested(); var firstFailed = false; try { var firstChance = image.LoadImageAsync(false, strategy, true); var firstTask = firstChance.AsTask(token); var c = await Task.WhenAny(Task.Delay(30_000), firstTask); if (c != firstTask) { firstFailed = true; firstChance.Cancel(); } } catch (Exception) { firstFailed = true; } if (firstFailed) { token.ThrowIfCancellationRequested(); await image.LoadImageAsync(false, strategy, true).AsTask(token); } progress.Report(new SaveGalleryProgress(Interlocked.Increment(ref loadedCount), this.Count)); } finally { semaphore.Release(); } })).ToArray(); await Task.WhenAll(loadTasks).ConfigureAwait(false); } using (var db = new GalleryDb()) { var gid = this.ID; var myModel = db.SavedSet.SingleOrDefault(model => model.GalleryId == gid); if (myModel is null) { db.SavedSet.Add(new SavedGalleryModel().Update(this)); } else { myModel.Update(this); } await db.SaveChangesAsync(); } }))); }