/// <summary> /// Schedules the image loading. If image is found in cache then it returns it, otherwise it loads it. /// </summary> /// <param name="task">Image loading task.</param> public async void LoadImage(IImageLoaderTask task) { Interlocked.Increment(ref _loadCount); if (_verbosePerformanceLogging && (_loadCount % 10) == 0) { LogSchedulerStats(); } if (task == null) { return; } if (task.IsCancelled) { task.Parameters?.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } if (task.Parameters.DelayInMs != null && task.Parameters.DelayInMs > 0) { await Task.Delay(task.Parameters.DelayInMs.Value).ConfigureAwait(false); } // If we have the image in memory then it's pointless to schedule the job: just display it straight away if (task.CanUseMemoryCache()) { var cacheResult = await task.TryLoadingFromCacheAsync().ConfigureAwait(false); if (cacheResult == CacheResult.Found) // If image is loaded from cache there is nothing to do here anymore { Interlocked.Increment(ref _statsTotalMemoryCacheHits); return; } if (cacheResult == CacheResult.ErrorOccured) // if something weird happened with the cache... error callback has already been called, let's just leave { return; } } _dispatch = _dispatch.ContinueWith(async t => { try { await LoadImageAsync(task).ConfigureAwait(false); } catch (Exception ex) { _logger.Error("An error occured while loading image", ex); } }); }
/// <summary> /// Schedules the image loading. If image is found in cache then it returns it, otherwise it loads it. /// </summary> /// <param name="task">Image loading task.</param> public async void LoadImage(IImageLoaderTask task) { if (task == null) { return; } if (task.IsCancelled) { task.Parameters.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } List <PendingTask> pendingTasksCopy; lock (_pendingTasksLock) { pendingTasksCopy = _pendingTasks.ToList(); } foreach (var pendingTask in pendingTasksCopy) { if (pendingTask.ImageLoadingTask != null && pendingTask.ImageLoadingTask.UsesSameNativeControl(task)) { pendingTask.ImageLoadingTask.CancelIfNeeded(); } } bool loadedFromCache = false; if (task.CanUseMemoryCache()) { loadedFromCache = await task.PrepareAndTryLoadingFromCacheAsync().ConfigureAwait(false); } if (loadedFromCache) { if (task.Parameters.OnFinish != null) { task.Parameters.OnFinish(task); } task.Dispose(); return; // image successfully loaded from cache } if (task.IsCancelled || _pauseWork) { task.Parameters.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } QueueAndGenerateImage(task); }
/// <summary> /// Schedules the image loading. If image is found in cache then it returns it, otherwise it loads it. /// </summary> /// <param name="task">Image loading task.</param> public async void LoadImage(IImageLoaderTask task) { if (task == null) { return; } if (task.Parameters.DelayInMs != null && task.Parameters.DelayInMs > 0) { await Task.Delay(task.Parameters.DelayInMs.Value).ConfigureAwait(false); } if (task.IsCancelled) { task.Parameters.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } // If we have the image in memory then it's pointless to schedule the job: just display it straight away if (task.CanUseMemoryCache()) { var cacheResult = await task.TryLoadingFromCacheAsync().ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave { return; // stop processing if loaded from cache OR if loading from cached raised an exception } } _dispatch = _dispatch.ContinueWith(async t => { try { await LoadImageAsync(task).ConfigureAwait(false); } catch (Exception ex) { _logger.Error("An error occured while loading image", ex); } }); }
/// <summary> /// Schedules the image loading. If image is found in cache then it returns it, otherwise it loads it. /// </summary> /// <param name="task">Image loading task.</param> public async void LoadImage(IImageLoaderTask task) { if (task == null) return; if (task.IsCancelled) { task.Parameters.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } List<PendingTask> pendingTasksCopy; lock (_pendingTasksLock) { pendingTasksCopy = _pendingTasks.ToList(); } foreach (var pendingTask in pendingTasksCopy) { if (pendingTask.ImageLoadingTask != null && pendingTask.ImageLoadingTask.UsesSameNativeControl(task)) pendingTask.ImageLoadingTask.CancelIfNeeded(); } bool loadedFromCache = false; if (task.CanUseMemoryCache()) { loadedFromCache = await task.PrepareAndTryLoadingFromCacheAsync().ConfigureAwait(false); } if (loadedFromCache) { if (task.Parameters.OnFinish != null) task.Parameters.OnFinish(task); task.Dispose(); return; // image successfully loaded from cache } if (task.IsCancelled || _pauseWork) { task.Parameters.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } QueueAndGenerateImage(task); }
/// <summary> /// Schedules the image loading. If image is found in cache then it returns it, otherwise it loads it. /// </summary> /// <param name="task">Image loading task.</param> public async void LoadImage(IImageLoaderTask task) { Interlocked.Increment(ref _loadCount); if (_verbosePerformanceLogging && (_loadCount % 10) == 0) { LogSchedulerStats(); } if (task == null) return; if (task.IsCancelled) { task.Parameters?.Dispose(); // this will ensure we don't keep a reference due to callbacks return; } if (task.Parameters.DelayInMs != null && task.Parameters.DelayInMs > 0) { await Task.Delay(task.Parameters.DelayInMs.Value).ConfigureAwait(false); } // If we have the image in memory then it's pointless to schedule the job: just display it straight away if (task.CanUseMemoryCache()) { var cacheResult = await task.TryLoadingFromCacheAsync().ConfigureAwait(false); if (cacheResult == CacheResult.Found) // If image is loaded from cache there is nothing to do here anymore { Interlocked.Increment(ref _statsTotalMemoryCacheHits); } if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // if something weird happened with the cache... error callback has already been called, let's just leave { if (task.Parameters.OnFinish != null) task.Parameters.OnFinish(task); task.Dispose(); return; } } else if (task?.Parameters?.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(task?.Parameters?.Path)) { _logger.Debug("ImageService: null path ignored"); return; } _dispatch = _dispatch.ContinueWith(async t => { try { await LoadImageAsync(task).ConfigureAwait(false); } catch (Exception ex) { _logger.Error("An error occured while loading image", ex); } }); }