private void reset() { status = DownloadFileStatus.INIT; error = null; contentBytes = 0; console_line = 0; FileName = null; file_stream = null; stream = null; }
public download_file(string dest_dir, int index) { this.web_client = new WebClient(); this.dest_dir = dest_dir; this.status = DownloadFileStatus.SUCCESS; this.indexThread = index; this.threadReset = new AutoResetEvent(false); this.thread = new Thread(downloadAsync); thread.Start(); }
private string getIcon(DownloadFileStatus st) { string res = null; switch (st) { case DownloadFileStatus.INIT: res = "+"; break; case DownloadFileStatus.DOWNLOAD: res = ">"; break; case DownloadFileStatus.SUCCESS: res = "*"; break; case DownloadFileStatus.ERROR: res = "E"; break; } return(string.Format("{0}", res)); }
/// <summary> /// Convert a <see cref="DownloadFileStatus"/> to <see cref="ImportFileStatus"/>. /// </summary> /// <param name="status"><see cref="DownloadFileStatus"/> to be converted.</param> /// <returns>Converted <see cref="ImportFileStatus"/>.</returns> private ImportFileStatus EnumConversion(DownloadFileStatus status) { switch (status) { case DownloadFileStatus.AlreadyExists: return(ImportFileStatus.AlreadyExists); case DownloadFileStatus.InvalidFile: return(ImportFileStatus.InvalidFile); case DownloadFileStatus.Downloaded: return(ImportFileStatus.SentToBeProcessed); default: return(ImportFileStatus.InvalidFile); } }
private void download_process() { status = DownloadFileStatus.DOWNLOAD; if (!stream.CanRead || (file_stream.Length == contentBytes)) { return; } if (file_stream.Length > 0 && !stream.CanSeek) { file_stream.SetLength(0); } int _count; long bytesPerTime = 0; long pos = file_stream.Position; byte[] buffer = new byte[512 * 1024]; long startTime = DateTime.Now.Ticks; if (pos != 0) { stream.Seek(pos, SeekOrigin.Begin); } while ((_count = stream.Read(buffer, 0, buffer.Length)) > 0) { file_stream.Write(buffer, 0, _count); pos += _count; bytesPerTime += _count; var seconds = TimeSpan.FromTicks(DateTime.Now.Ticks - startTime).TotalSeconds; if (seconds > 0.35) { double speed = (double)bytesPerTime / seconds; bytesPerTime = 0; startTime = DateTime.Now.Ticks; if (currentAsyncResult.IsCompleted) { DownloadProgress.EndInvoke(currentAsyncResult); currentAsyncResult = DownloadProgress.BeginInvoke(this, new DownloadProgressEventArgs(this, pos, speed), null, null); } } } }
private void start_download() { currentAsyncResult = DownloadProgress.BeginInvoke(this, new DownloadProgressEventArgs(this, 0, 0), null, null); try { if (stream == null || Error != null) { return; } if (!stream.CanRead) { error = new Exception("Url has no access to read"); return; } init_file_stream(); download_process(); } catch (Exception ex) { error = ex; } finally { if (file_stream != null) { file_stream.Close(); } if (stream != null) { stream.Close(); } status = error == null ? DownloadFileStatus.SUCCESS : DownloadFileStatus.ERROR; if (!currentAsyncResult.IsCompleted) { DownloadProgress.EndInvoke(currentAsyncResult); } DownloadCompleted.Invoke(this); } }
private string getIcon(DownloadFileStatus st) { string res = null; switch (st) { case DownloadFileStatus.INIT: res = "+"; break; case DownloadFileStatus.DOWNLOAD: res = ">"; break; case DownloadFileStatus.SUCCESS: res = "*"; break; case DownloadFileStatus.ERROR: res = "E"; break; } return string.Format("{0}", res); }
private void start_download() { currentAsyncResult = DownloadProgress.BeginInvoke(this, new DownloadProgressEventArgs(this, 0, 0), null, null); try { if (stream == null || Error != null) return; if (!stream.CanRead) { error = new Exception("Url has no access to read"); return; } init_file_stream(); download_process(); } catch (Exception ex) { error = ex; } finally { if (file_stream != null) file_stream.Close(); if (stream != null) stream.Close(); status = error == null ? DownloadFileStatus.SUCCESS : DownloadFileStatus.ERROR; if (!currentAsyncResult.IsCompleted) DownloadProgress.EndInvoke(currentAsyncResult); DownloadCompleted.Invoke(this); } }
private void download_process() { status = DownloadFileStatus.DOWNLOAD; if (!stream.CanRead || (file_stream.Length == contentBytes)) return; if (file_stream.Length > 0 && !stream.CanSeek) { file_stream.SetLength(0); } int _count; long bytesPerTime = 0; long pos = file_stream.Position; byte[] buffer = new byte[512 * 1024]; long startTime = DateTime.Now.Ticks; if (pos != 0) stream.Seek(pos, SeekOrigin.Begin); while ((_count = stream.Read(buffer, 0, buffer.Length)) > 0) { file_stream.Write(buffer, 0, _count); pos += _count; bytesPerTime += _count; var seconds = TimeSpan.FromTicks(DateTime.Now.Ticks - startTime).TotalSeconds; if (seconds > 0.35) { double speed = (double)bytesPerTime / seconds; bytesPerTime = 0; startTime = DateTime.Now.Ticks; if (currentAsyncResult.IsCompleted) { DownloadProgress.EndInvoke(currentAsyncResult); currentAsyncResult = DownloadProgress.BeginInvoke(this, new DownloadProgressEventArgs(this, pos, speed), null, null); } } } }