/// <summary> /// cancel a background task - or prevent a task from running /// </summary> public void Cancel() { lock (_lock) { if (_client != null) { _client.CancelAsync(); } else { // probably an unstarted task or a task that has completed _complete = true; TaskComplete.Set(); } } }
void ClientDownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { StatusUpdateEventArgs args = null; lock (_lock) { var syncItem = e.UserState as SyncItem; if (syncItem == null) { args = new StatusUpdateEventArgs(StatusUpdateLevel.Error, "Missing token from download completed", false, null); } else if (e.Cancelled) { args = new StatusUpdateEventArgs(StatusUpdateLevel.Status, string.Format(CultureInfo.InvariantCulture, "{0} Cancelled", syncItem.EpisodeTitle), false, syncItem); } else if (e.Error != null && e.Error.InnerException != null) { args = new StatusUpdateEventArgs(StatusUpdateLevel.Error, string.Format(CultureInfo.InvariantCulture, "Error in: {0}", syncItem.EpisodeTitle), e.Error.InnerException, false, syncItem); } else if (e.Error != null) { args = new StatusUpdateEventArgs(StatusUpdateLevel.Error, string.Format(CultureInfo.InvariantCulture, "Error in: {0}", syncItem.EpisodeTitle), e.Error, false, syncItem); } else { args = new StatusUpdateEventArgs(StatusUpdateLevel.Status, string.Format(CultureInfo.InvariantCulture, "{0} Completed", syncItem.EpisodeTitle), true, syncItem); _fileUtilities.FileRename(GetDownloadFilename(), _syncItem.DestinationPath, true); RecordHighTideMark(syncItem); ExecutePostDownloadCommand(); } OnStatusUpdate(args); _client.Dispose(); _client = null; _complete = true; _counterFactory.CreateAverageCounter(Constants.PodcastUtilitiesCommonCounterCategory, Constants.AverageTimeToDownload, Constants.NumberOfDownloads).RegisterTime(_stopWatch); _counterFactory.CreateAverageCounter(Constants.PodcastUtilitiesCommonCounterCategory, Constants.AverageMBDownload, Constants.SizeOfDownloads).RegisterValue(ConvertBytesToMB(_bytesDownloaded)); TaskComplete.Set(); } }
public void ForceComplete() { _complete = true; TaskComplete.Set(); }