private void backgroundworker_download_DoWork(object sender, DoWorkEventArgs e) { var chapters = mChapers; var pb = progressbar_download; var label = label_downloadinfo; string savePath = e.Argument.ToString(); List <ChapterInfo> chaperInfoList = new List <ChapterInfo>(); for (int i = 0; i < chapters.Length; i++) { if (backgroundworker_download.CancellationPending) { return; } var chapter = chapters[i]; float progress = (float)(i + 1) / (float)chapters.Length; string info = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length, progress * 100); backgroundworker_download.ReportProgress(i, info); try { var chapterInfo = LibZhuiShu.getChapter(chapter.link); if (chapterInfo != null) { chaperInfoList.Add(chapterInfo); } else { MessageBox.Show("下载失败:" + chapter.title); return; } } catch (Exception exc) { MessageBox.Show("下载失败,请切换书源后重试:" + exc); return; } } backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后...."); string ext = Path.GetExtension(savePath); Book book = new Book(); book.name = mBook.title; book.author = mBook.author; book.id = mBook._id; book.chapters = chaperInfoList.ToArray(); if (ext.ToLower() == ".txt") { Kindlegen.book2Txt(book, savePath); } else if (ext.ToLower() == ".mobi") { Kindlegen.book2Mobi(book, savePath); } MessageBox.Show("下载完成,文件保存在:" + savePath); }
private void backgroundworker_download_DoWork(object sender, DoWorkEventArgs e) { var chapters = mChapers; var pb = progressbar_download; var label = label_downloadinfo; string savePath = e.Argument.ToString(); List <ChapterInfo> chaperInfoList = new List <ChapterInfo>(); for (int i = 0; i < chapters.Length; i++) { if (backgroundworker_download.CancellationPending) { return; } var chapter = chapters[i]; float progress = (float)(i + 1) / (float)chapters.Length; string info = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length, progress * 100); backgroundworker_download.ReportProgress(i, info); while (true) { bool downloadSucess = false; string errMsg = ""; for (int j = 0; j < 3; j++) { try { var chapterInfo = LibZhuiShu.getChapter(chapter.link); if (chapterInfo != null) { chaperInfoList.Add(chapterInfo); downloadSucess = true; break; } } catch (Exception ex) { errMsg = ex.Message; } } if (!downloadSucess) { var result = MessageBox.Show(errMsg, "章节 " + chapter.title + " 下载失败", MessageBoxButtons.AbortRetryIgnore); if (result == DialogResult.Abort) { return; } else if (result == DialogResult.Ignore) { var emptyChaper = new ChapterInfo(); emptyChaper.title = chapter.title; emptyChaper.body = "本章下载失败了,失败原因:\n " + errMsg; chaperInfoList.Add(emptyChaper); downloadSucess = true; break; } } else { break; } } } backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后...."); string ext = Path.GetExtension(savePath); Book book = new Book(); book.name = mBook.title; book.author = mBook.author; book.id = mBook._id; book.chapters = chaperInfoList.ToArray(); if (ext.ToLower() == ".txt") { Kindlegen.book2Txt(book, savePath); } else if (ext.ToLower() == ".mobi") { Kindlegen.book2Mobi(book, savePath); } MessageBox.Show("下载完成,文件保存在:" + savePath); }