public DownloadTask(Downloader downloader, Uri url, Action<DownloadResult> callback, DownloadOptions options, string key, DownloadResult result) { this.downloader = downloader; this.Url = url; this.Callback = callback; this.Options = options; this.Key = key; this.Result = result; }
/// <summary> /// Cancels a pending or in-progress download operation. /// </summary> /// <param name="result">The <see cref="DownloadResult"/> representing the operation to cancel.</param> /// <returns>True if the operation was found and cancelled, false otherwise.</returns> public bool Cancel(DownloadResult result) { if (this.disposed) { throw new ObjectDisposedException(this.GetType().Name); } if (result == null) { throw new ArgumentNullException("result", "result cannot be null."); } bool cancelled = false; DownloadTask task = null; lock (this.syncRoot) { task = this.processing.Where(t => t.Result == result).FirstOrDefault(); if (task == null) { task = this.queued.Where(t => t.Result == result).FirstOrDefault(); if (task != null) { this.queued.Remove(task); } } if (task != null) { task.Abort(); cancelled = true; } } return cancelled; }
/// <summary> /// Initiates a cached + queued download operation. /// </summary> /// <param name="url">The URL of the resource to download.</param> /// <param name="callback">A function to call when the operation is complete.</param> /// <param name="options">The options to use when downloading the resource.</param> /// <returns>The operation's result.</returns> public DownloadResult Download(Uri url, Action<DownloadResult> callback, DownloadOptions options) { if (this.disposed) { throw new ObjectDisposedException(this.GetType().Name); } if (url == null) { throw new ArgumentNullException("url", "url cannot be null."); } DownloadResult result; string key = url.ToString().ToUpperInvariant(); byte[] content = this.cache.GetContent(key); if (content != null) { result = new DownloadResult(content, true); } else { result = new DownloadResult(); options = new DownloadOptions(options); this.Enqueue(new DownloadTask(this, url, callback, options, key, result)); } return result; }