public async Task <HttpResponseMessage> GetNewsAsync() { using (var cts = new CancellationTokenSource()) { RunningTasks.Add(RemoteRequestAsync(redditApi.GetApi(Priority.UserInitiated).GetNews(cts.Token)).Id, cts); return(await RemoteRequestAsync(redditApi.GetApi(Priority.UserInitiated).GetNews(cts.Token))); } }
public async Task <HttpResponseMessage> GetMakeUpsAsync(string brand) { using (var cts = new CancellationTokenSource()) { Task <HttpResponseMessage> task = RemoteRequestAsync(makeUpApi.GetApi(Priority.UserInitiated).GetMakeUps(brand)); RunningTasks.Add(task.Id, cts); return(await task); } }
public async Task <int> AddMakeupLocalDataAsync(List <MakeUpModel> makeUpModelList) { using var cts = new CancellationTokenSource(); Task <int> task = makeUpApi.InsertAll(AutoMapper.Map <List <MakeUp> >(makeUpModelList)); RunningTasks.Add(task.Id, cts); return(await task); }
public async Task <List <MakeUpModel> > GetMakeupLocalDataAsync() { using (var cts = new CancellationTokenSource()) { Task <List <MakeUp> > task = makeUpApi.Get(); RunningTasks.Add(task.Id, cts); var makeUpModels = Task.Run(() => AutoMapper.Map <List <MakeUpModel> >(task.Result)); RunningTasks.Add(makeUpModels.Id, cts); return(await makeUpModels); } }
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); } }
public async Task <List <MakeUpModel> > GetMakeupLocalUsingEFDataAsync() { using (var cts = new CancellationTokenSource()) { var makeUp = new MakeUp() { Brand = "xxxxx", Category = "xxxxxxx" }; MakeUp value = UnitOfWork.GenericHandler <MakeUp>().Insert(makeUp); UnitOfWork.Save(); Task <List <MakeUp> > task = makeUpApi.Get(); RunningTasks.Add(task.Id, cts); var makeUpModels = Task.Run(() => AutoMapper.Map <List <MakeUpModel> >(task.Result)); RunningTasks.Add(makeUpModels.Id, cts); return(await makeUpModels); } }
protected async Task TakeFromPendingTasksAndRunAsync() { if (PendingTasks.Count == 0) { return; } Dictionary <string, IImageLoaderTask> tasksToRun = null; int preloadOrUrlTasksCount = 0; int urlTasksCount = 0; int preloadTasksCount = 0; lock (_lock) { if (RunningTasks.Count >= MaxParallelTasks) { urlTasksCount = RunningTasks.Count(v => v.Value != null && (!v.Value.Parameters.Preload && v.Value.Parameters.Source == ImageSource.Url)); preloadTasksCount = RunningTasks.Count(v => v.Value != null && v.Value.Parameters.Preload); preloadOrUrlTasksCount = preloadTasksCount + urlTasksCount; if (preloadOrUrlTasksCount == 0 || preloadOrUrlTasksCount != MaxParallelTasks) { return; } // Allow only half of MaxParallelTasks as additional allowed tasks when preloading occurs to prevent starvation if (RunningTasks.Count - Math.Max(1, Math.Min(preloadOrUrlTasksCount, MaxParallelTasks / 2)) >= MaxParallelTasks) { return; } } int numberOfTasks = MaxParallelTasks - RunningTasks.Count + Math.Min(preloadOrUrlTasksCount, MaxParallelTasks / 2); tasksToRun = new Dictionary <string, IImageLoaderTask>(); IImageLoaderTask task = null; while (tasksToRun.Count < numberOfTasks && PendingTasks.TryDequeue(out task)) { if (task == null || task.IsCancelled || task.IsCompleted) { continue; } // We don't want to load, at the same time, images that have same key or same raw key at the same time // This way we prevent concurrent downloads and benefit from caches string rawKey = task.KeyRaw; if (RunningTasks.ContainsKey(rawKey) || tasksToRun.ContainsKey(rawKey)) { SimilarTasks.Add(task); continue; } if (preloadOrUrlTasksCount != 0) { if (!task.Parameters.Preload && (urlTasksCount == 0 || task.Parameters.Source != ImageSource.Url)) { tasksToRun.Add(rawKey, task); } else { Enqueue(task); break; } } else { tasksToRun.Add(rawKey, task); } } foreach (var item in tasksToRun) { RunningTasks.Add(item.Key, item.Value); Interlocked.Increment(ref _statsTotalRunning); } } if (tasksToRun != null && tasksToRun.Count > 0) { var tasks = tasksToRun.Select(p => RunImageLoadingTaskAsync(p.Value)); await Task.WhenAll(tasks).ConfigureAwait(false); } }
/// <summary> /// Asynchronously runs <paramref name="delegateFunction"/> for every item in the given collection /// </summary> /// <typeparam name="T">Type of the collection item</typeparam> /// <param name="delegateFunction">The function to asynchronously run</param> /// <param name="collection">The collection to be enumerated</param> /// <exception cref="InvalidOperationException">Thrown if execution starts before the end of another operation</exception> public async Task RunForEach <T>(IEnumerable <T> collection, ForEachItemAsync <T> delegateFunction) { if (RunningTasks.Count > 0) { throw new InvalidOperationException(Properties.Resources.AsyncFor_ErrorNoConcurrentExecution); } var exceptions = new List <Exception>(); var taskItemQueue = new Queue <T>(); foreach (var item in collection) { taskItemQueue.Enqueue(item); } TotalTasks = taskItemQueue.Count; // While there's either more tasks to start or while there's still tasks running while (taskItemQueue.Count > 0 || (taskItemQueue.Count == 0 && RunningTasks.Count > 0)) { if ((BatchSize < 1 || RunningTasks.Count < BatchSize) && taskItemQueue.Count > 0) { // We can run more tasks // Get the next task item to run var item = taskItemQueue.Dequeue(); // The item in Collection to process // Start the task var tTask = Task.Run(async() => { await delegateFunction(item).ConfigureAwait(false); IncrementCompletedTasks(); }); // Either wait for it or move on if (RunSynchronously) { await tTask.ConfigureAwait(false); } else { RunningTasks.Add(tTask); } } else { if (RunningTasks.Count > 0) { // We can't start any more tasks, so we have to wait on one. await Task.WhenAny(RunningTasks).ConfigureAwait(false); // Remove completed tasks for (var count = RunningTasks.Count - 1; count >= 0; count--) { if (RunningTasks[count].Exception != null) { var ex = RunningTasks[count].Exception; if (ex is AggregateException aggregateEx && aggregateEx.InnerExceptions.Count == 1) { exceptions.Add(aggregateEx.InnerExceptions.First()); } else { exceptions.Add(RunningTasks[count].Exception); } RunningTasks.RemoveAt(count); } else if (RunningTasks[count].IsCompleted) { RunningTasks.RemoveAt(count); } } }
protected async Task TakeFromPendingTasksAndRunAsync() { if (PendingTasks.Count == 0) { return; } Dictionary <string, IImageLoaderTask>?tasksToRun = null; int urlTasksCount = 0; lock (_lock) { if (RunningTasks.Count >= MaxParallelTasks) { urlTasksCount = RunningTasks.Count(v => v.Value != null && v.Value.ImageSource.Source is UriSource); if (urlTasksCount == 0 || urlTasksCount != MaxParallelTasks) { return; } // Allow only half of MaxParallelTasks as additional allowed tasks when preloading occurs to prevent starvation if (RunningTasks.Count - Math.Max(1, Math.Min(urlTasksCount, MaxParallelTasks / 2)) >= MaxParallelTasks) { return; } } int numberOfTasks = MaxParallelTasks - RunningTasks.Count + Math.Min(urlTasksCount, MaxParallelTasks / 2); tasksToRun = new Dictionary <string, IImageLoaderTask>(); while (tasksToRun.Count < numberOfTasks && PendingTasks.TryDequeue(out IImageLoaderTask? task)) { if (task == null || task.IsCancelled || task.IsCompleted) { continue; } // We don't want to load, at the same time, images that have same key or same raw key at the same time // This way we prevent concurrent downloads and benefit from caches string rawKey = task.KeyRaw; if (RunningTasks.ContainsKey(rawKey) || tasksToRun.ContainsKey(rawKey)) { SimilarTasks.Add(task); continue; } if (urlTasksCount != 0) { if (!(task.ImageSource.Source is UriSource)) { tasksToRun.Add(rawKey, task); } else { Enqueue(task); break; } } else { tasksToRun.Add(rawKey, task); } } foreach (var item in tasksToRun) { RunningTasks.Add(item.Key, item.Value); Interlocked.Increment(ref _statsTotalRunning); } } if (tasksToRun != null && tasksToRun.Count > 0) { var tasks = tasksToRun.Select(async p => { await Task.Factory.StartNew(async() => { try { await RunImageLoadingTaskAsync(p.Value).ConfigureAwait(false); } catch (Exception ex) { _imageLoader.Logger?.Error("TakeFromPendingTasksAndRun exception", ex); } }, CancellationToken.None, TaskCreationOptions.PreferFairness | TaskCreationOptions.DenyChildAttach | TaskCreationOptions.HideScheduler, TaskScheduler.Default).ConfigureAwait(false); }); await Task.WhenAll(tasks).ConfigureAwait(false); } }
public void SubmitTask(Task task) { RunningTasks.Add(task); }