protected override void Dispose(bool finalize) { if (_result != null) { try { _result.EndInvoke(); } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { ErrorOccured.Raise(this, new ChoExceptionEventArgs(ex)); } finally { _result = null; IChoAsyncResult result = ChoConsole.OutputQueuedExecutionService.Enqueue(() => { //Reset to old settings Console.ForegroundColor = ChoConsole.DefaultConsoleForegroundColor; Console.BackgroundColor = ChoConsole.DefaultConsoleBackgroundColor; Console.SetCursorPosition(0, _statusMsgLocation.Y + 1); Console.WriteLine(); }); result.AsyncWaitHandle.WaitOne(); } } }
private void cd_Failed(object sender, ErrorEventArgs e) { if (FlagStop) { return; } var ex = e.GetException(); if (ex is ReturnedContentSizeWrongException) { if (Ranges.Count == 2) { foreach (var cd in cdList) { Info.AcceptRanges = false; cd.Info.AcceptRanges = false; cd.UseChunk = false; } Ranges.RemoveAt(1); Ranges[0].End = Info.ContentSize - 1; foreach (var item in cdList.Where(x => x.Wait)) { item.Wait = false; } return; } } ErrorOccured.Raise(sender, e, aop); }
private void Process() { try { if (Info == null) { State = Status.GettingHeaders; GetHeaders(); aop.Post(delegate { DownloadInfoReceived.Raise(this, EventArgs.Empty); }, null); } var append = RemainingRangeStart > 0; var bytesRead = 0; var buffer = new byte[2 * 1024]; State = Status.SendingRequest; using (var response = RequestHelper.CreateRequestGetResponse(Info, RemainingRangeStart, BeforeSendingRequest, AfterGettingResponse)) { State = Status.GettingResponse; using (var responseStream = response.GetResponseStream()) using (var file = FileHelper.CheckFile(FullFileName, append)) { while (allowDownload && (bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0) { State = Status.Downloading; file.Write(buffer, 0, bytesRead); TotalBytesReceived += bytesRead; speedBytesTotal += bytesRead; aop.Post(delegate { ProgressChanged.Raise(this, new ProgressChangedEventArgs(SpeedInBytes, this.Progress, TotalBytesReceived)); }, Progress); } } } if (RemainingRangeStart > Info.Length) { throw new Exception("Total bytes received overflows content length"); } if (TotalBytesReceived == Info.Length) { State = Status.Completed; DownloadCompleted.Raise(this, EventArgs.Empty); } else if (!allowDownload) { State = Status.Paused; } } catch (Exception ex) { State = Status.ErrorOccured; aop.Post(delegate { ErrorOccured.Raise(this, new ErrorEventArgs(ex)); }, null); } }
/// <summary> /// Resumes all threads after stopping /// <exception cref="AltoMultiThreadDownloader.Exceptions.RemoteFilePropertiesChangedException">Thrown when download informations changed, in case of expired url or changed authentication data</exception> /// </summary> public void Resume() { var currentInfo = GetCurrentInformations(); if (currentInfo == null || !currentInfo.Equals(this.Info)) { ErrorOccured.Raise(this, new ErrorEventArgs( new RemoteFilePropertiesChangedException(this.Info, currentInfo)), aop); return; } speedBytesOffset = TotalBytesReceived; FlagStop = false; stp.Reset(); stp.Start(); createNewThreadIfRequired(); Resumed.Raise(this, EventArgs.Empty, aop); }
private void Process() { try { //Get the download headers if not exists if (Info == null) { State = Status.GettingHeaders; GetHeaders(); aop.Post(delegate { DownloadInfoReceived.Raise(this, EventArgs.Empty); }, null); } //Gets where download left var append = RemainingRangeStart > 0; var bytesRead = 0; var buffer = new byte[2 * 1024]; State = Status.SendingRequest; using (var response = RequestHelper.CreateRequestGetResponse(Info, RemainingRangeStart, BeforeSendingRequest, AfterGettingResponse, Info.IsChunked)) { State = Status.GettingResponse; using (var responseStream = response.GetResponseStream()) using (var file = FileHelper.CheckFile(FullFileName, append, LastMD5Checksum)) { while (allowDownload && (bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0) { State = Status.Downloading; file.Write(buffer, 0, bytesRead); TotalBytesReceived += bytesRead; speedBytesTotal += bytesRead; aop.Post(delegate { ProgressChanged.Raise(this, new ProgressChangedEventArgs(SpeedInBytes, this.Progress, TotalBytesReceived)); }, Progress); } } } if (Info.Length > 0 && RemainingRangeStart > Info.Length) { throw new Exception("Total bytes received overflows content length"); } if (TotalBytesReceived == Info.Length || (Info.Length < 1 && allowDownload)) { State = Status.Completed; DownloadCompleted.Raise(this, EventArgs.Empty); } else if (!allowDownload) { State = Status.Paused; DownloadPaused.Raise(this, EventArgs.Empty); } } catch (Exception ex) { if (TotalBytesReceived == Info.Length) { State = Status.Completed; DownloadCompleted.Raise(this, EventArgs.Empty); } else if (!allowDownload) { State = Status.Paused; DownloadPaused.Raise(this, EventArgs.Empty); } else { LastError = ex; State = Status.ErrorOccured; aop.Post(delegate { ErrorOccured.Raise(this, new ErrorEventArgs(ex)); }, null); } } finally { if (LastError == null || !(LastError is FileValidationFailedException)) { LastMD5Checksum = FileHelper.CalculateMD5(FullFileName); } } }