public void Add(Downloader downloader, bool autoStart) { downloader.StateChanged += new EventHandler(downloader_StateChanged); using (LockDownloadList(true)) { downloads.Add(downloader); } OnDownloadAdded(downloader, autoStart); if (autoStart) { downloader.Start(); } }
public void Add(Downloader downloader, bool autoStart) { downloader.StateChanged += new EventHandler(downloader_StateChanged); using (LockDownloadList(true)) { //downloads.Add(downloader); downloader.OrderedIndex = orderedDownloads.Count; downloads.Add(downloader.Id, downloader); orderedDownloads.Add(downloader.Id); } OnDownloadAdded(downloader, autoStart); if (autoStart) { downloader.Start(); } }
/// <summary> /// class-internal method to perform a download with mirrors. Has blocking wait and sets ITask status to FAIL /// in case of failure. /// </summary> /// <param name="urlPath">full URL of file, optionally leaving out protocol http://</param> /// <param name="filename">local name under which to store the file</param> /// <param name="toLocalFolder">local folder where to store file</param> /// <param name="mirrors">optional set of mirrors for urlPath, may be empty string[] for none</param> /// <param name="overwriteExisting">if true, overwrites any existing file 'filename'</param> protected void InternalDoDownload(string urlPath, string filename, string toLocalFolder, bool overwriteExisting, string[] mirrors ) { localFile = toLocalFolder + "\\" + filename; // check if file already there and overwriting is unwanted. if (File.Exists(localFile) && !overwriteExisting) { status = ITaskStatus.SUCCESS; return; // yes we're done! no download needed } // make sure protocol is specified if (!urlPath.Contains("://")) urlPath = "http://" + urlPath; // construct temp file names string tempFile; tempFile = Path.GetTempPath() + "IndiegameGarden_" + Path.GetRandomFileName(); TryDeleteFile(tempFile); downloader = DownloadManager.Instance.Add( ResourceLocation.FromURL(urlPath), ResourceLocation.FromURLArray(mirrors), tempFile, segmentsUsedInDownload, false); if (downloader != null) { downloader.MaxRetries = MaxRetries; downloader.Start(); if (downloader != null) { downloader.WaitForConclusion(); if (downloader == null) // case may happen! (on basedownloader cleanup in other thread) { status = ITaskStatus.FAIL; statusMsg = "Download aborted"; } else if (!downloader.State.Equals(DownloaderState.Ended)) { if (downloader.LastError != null) statusMsg = downloader.LastError.Message; else statusMsg = "Download aborted or timed out"; status = ITaskStatus.FAIL; } else status = ITaskStatus.SUCCESS; // remove the temp file if failed if (File.Exists(tempFile) && !IsSuccess()) { TryDeleteFile(tempFile); } // move temp file to localFile on success else if (IsSuccess()) { try { TryDeleteFile(localFile); File.Move(tempFile, localFile); status = ITaskStatus.SUCCESS; } catch (Exception ex) { status = ITaskStatus.FAIL; statusMsg = "Couldn't move downloaded file to " + localFile + ": " + ex.ToString(); } finally { TryDeleteFile(tempFile); } } } } else { status = ITaskStatus.FAIL; statusMsg = "failed to create downloader by DownloadManager."; } }