async Task LoadMoreNewerAsync() { await DetectThreadService.LogAsync(); if (CurrentPageNumber < MaxPageNumber) { var realm = Realm.GetInstance(); var currentThread = realm.All <SettingsRealmObject>().First().CurrentNode.CurrentThread; var lastCommentId = ItemsSource.Last().CommentId; MaxPageNumber = await currentThread.UpdateAsync(++CurrentPageNumber); var newItemsSource = currentThread.Comments.OrderByDescending(o => o.CommentDateTime).ToList(); var lastComment = newItemsSource.Where(o => o.CommentId == lastCommentId).First(); var newItemSourceBeginIndex = newItemsSource.IndexOf(lastComment) + 1; System.Diagnostics.Debug.WriteLine("newItemSourceBeginIndex: " + newItemSourceBeginIndex); await Device.InvokeOnMainThreadAsync(() => { for (var i = newItemSourceBeginIndex; i < newItemsSource.Count; i++) { if (i < newItemsSource.Count) { ItemsSource.Add(newItemsSource[i]); } } }); System.Diagnostics.Debug.WriteLine("New Page: " + CurrentPageNumber); System.Diagnostics.Debug.WriteLine("New ItemsSource.Count: " + ItemsSource.Count); } }
void ProcessQueue() { Task.Run(async() => { await DetectThreadService.LogAsync(); int index; // This variable has no meaning var value = new object(); while (true) { await Task.Delay(1000); if (!RequiredDownloadingCellIndexesQueue.TryPeek(out index)) { System.Diagnostics.Debug.WriteLine("Skipped because the queue is empty."); continue; } if (Interlocked.Increment(ref DownloadingCount) > MaxDownloadingCount) { Interlocked.Decrement(ref DownloadingCount); System.Diagnostics.Debug.WriteLine("Skipped because over max concurrent updating."); continue; } var _ = Task.Run(async() => { try { System.Diagnostics.Debug.WriteLine("Update: " + index); await ItemsSource[index].UpdateAttachment(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception has occurred: " + index); System.Diagnostics.Debug.WriteLine("Message: " + e.Message); System.Diagnostics.Debug.WriteLine("StackTrace: " + e.StackTrace); if (e.InnerException != null) { System.Diagnostics.Debug.WriteLine("InnerException Message: " + e.InnerException.Message); System.Diagnostics.Debug.WriteLine("InnerException StackTrace: " + e.InnerException.StackTrace); } } finally { RequiredDownloadingCellIndexesQueue.TryDequeue(out index); Interlocked.Decrement(ref DownloadingCount); RequiredDownloadingCellIndexesDictionary.TryRemove(index, out value); } }); } }); }