public void CancelDownload() { _downloadCanceled = true; if (_wc != null && _wc.IsBusy) { _wc.CancelAsync(); } Logger.Log("Download canceled"); if (DownloadCanceled != null) { Task.Run(() => DownloadCanceled.Invoke(null)); } }
// ダウンロード完了 private async Task OnDownloadCompleted(Task <DownloadOperation> prevTask) { if (prevTask.IsFaulted) { return; } Debug.WriteLine("キャッシュ完了"); if (prevTask.Result != null) { var op = prevTask.Result; var info = NiconicoMediaManager.CacheRequestInfoFromFileName(op.ResultFile); await RemoveDownloadOperation(info); if (op.Progress.Status == BackgroundTransferStatus.Completed) { if (op.Progress.TotalBytesToReceive == op.Progress.BytesReceived) { Debug.WriteLine("キャッシュ済み: " + op.ResultFile.Name); DownloadCompleted?.Invoke(this, info, op.ResultFile.Path); } else { Debug.WriteLine("キャッシュキャンセル: " + op.ResultFile.Name); DownloadCanceled?.Invoke(this, info); } using (var pendingVideoLockReleaser = await _CacheDownloadPendingVideosLock.LockAsync()) { _CacheDownloadPendingVideos.Remove(info); } } else { Debug.WriteLine($"キャッシュ失敗: {op.ResultFile.Name} (再ダウンロードします)"); DownloadCanceled?.Invoke(this, info); await AddCacheRequest(info.RawVideoId, info.Quality, forceUpdate : true); } try { if (op.TransferGroup != null && _BackgroundTransferCompletionInfoMap.ContainsKey(op.TransferGroup.Name) ) { var btcInfo = _BackgroundTransferCompletionInfoMap[op.TransferGroup.Name]; btcInfo.TaskRegistration.Unregister(cancelTask: false); _BackgroundTransferCompletionInfoMap.Remove(op.TransferGroup.Name); } } catch { Debug.WriteLine("failed unregister background download completion task."); } } await TryNextCacheRequestedVideoDownload(); }
protected void OnDownloadCanceled(DownloadCanceledEventArgs e) { DownloadCanceled?.Invoke(this, e); DownloadsUpdated?.Invoke(this); }
private void _wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { bool errorOccured = false; if (e.Error != null) { errorOccured = true; string text = "Error occured while downloading dependency " + _currentDependency.Name + ": \n" + e.Error.Message; if (!_downloadCanceled) { Logger.Log("Error while doanloading dependency: " + _currentDependency.Name, Logger.LogLevel.error); Logger.Log("Error was: " + e.Error.Message, Logger.LogLevel.error); } if (e.Error is System.Net.WebException) { if (_downloadCanceled) { text = null; } else { CancelDownload(); } } Exception ex = null; if (text != null) { ex = new Exception(text); } if (DownloadCanceled != null) { Task.Run(() => DownloadCanceled.Invoke(ex)); } cleanup(); return; } Logger.Log("Download complete: " + _currentDependency.Name); if (_downloadCanceled || errorOccured) { return; } Task.Run(() => { Logger.Log("Extracting dependency: " + _currentDependency.Name); if (ExtractionStarted != null) { Task.Run(() => ExtractionStarted(_currentDependency.Name)); } try { extractDependency(); } catch (Exception ex) { Logger.Log("Error while removing temp directory: " + ex.Message, Logger.LogLevel.error); if (ErrorOccured != null) { Task.Run(() => ErrorOccured.Invoke(ex)); } } }); }