Task AppendAwaitCompletion(Task theTask) { if (!theTask.IsCompleted) { var updatedTask = theTask.ContinueWith(t => RunningTasks.Remove(t.Id, out _)); return(RunningTasks.AddOrUpdate(theTask.Id, updatedTask, (_, t) => updatedTask)); } return(theTask); }
protected async Task RunImageLoadingTaskAsync(IImageLoaderTask pendingTask) { string keyRaw = pendingTask.KeyRaw; try { if (_imageLoader.VerbosePerformanceLogging) { IPlatformPerformance performance = _imageLoader.Performance; LogSchedulerStats(performance); var stopwatch = Stopwatch.StartNew(); await pendingTask.RunAsync().ConfigureAwait(false); stopwatch.Stop(); _imageLoader.Logger?.Debug(string.Format("[PERFORMANCE] RunAsync - NetManagedThreadId: {0}, NativeThreadId: {1}, Execution: {2} ms, Key: {3}", performance.GetCurrentManagedThreadId(), performance.GetCurrentSystemThreadId(), stopwatch.Elapsed.Milliseconds, pendingTask.Key)); } else { await pendingTask.RunAsync().ConfigureAwait(false); } } finally { lock (_lock) { RunningTasks.Remove(keyRaw); if (SimilarTasks.Count > 0) { SimilarTasks.RemoveAll(v => v == null || v.IsCompleted || v.IsCancelled); var similarItems = SimilarTasks.Where(v => v.KeyRaw == keyRaw); foreach (var similar in similarItems) { SimilarTasks.Remove(similar); LoadImage(similar); } } } pendingTask.TryDispose(); await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false); } }
protected async Task RunImageLoadingTaskAsync(PendingTask pendingTask) { var key = pendingTask.ImageLoadingTask.Key; lock (_pendingTasksLock) { if (RunningTasks.ContainsKey(key)) return; RunningTasks.Add(key, pendingTask); Interlocked.Increment(ref _statsTotalRunning); } try { if (Configuration.VerbosePerformanceLogging) { LogSchedulerStats(); var stopwatch = Stopwatch.StartNew(); await Task.Run(pendingTask.ImageLoadingTask.RunAsync).ConfigureAwait(false); stopwatch.Stop(); Logger.Debug( string.Format( "[PERFORMANCE] RunAsync - NetManagedThreadId: {0}, NativeThreadId: {1}, Execution: {2} ms, Key: {3}", Performance.GetCurrentManagedThreadId(), Performance.GetCurrentSystemThreadId(), stopwatch.Elapsed.Milliseconds, key)); } else { await Task.Run(pendingTask.ImageLoadingTask.RunAsync).ConfigureAwait(false); } } finally { lock (_pendingTasksLock) { RunningTasks.Remove(key); } pendingTask?.ImageLoadingTask?.Dispose(); await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false); } }