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 = (i + 1) / (float)chapters.Length;
                string info     = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length + 1,
                                                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;
                            loglibrary.LogHelper.Error(ex);
                            loglibrary.LogHelper.Flush();
                        }
                    }
                    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()
                            {
                                title = chapter.title,
                                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()
            {
                name     = mBook.title,
                author   = mBook.author,
                id       = mBook._id,
                chapters = chaperInfoList.ToArray()
            };

            if (ext.ToLower() == ".txt")
            {
                Kindlegen.Book2Txt(book, savePath);
            }
            else if (ext.ToLower() == ".mobi")
            {
                Kindlegen.Book2Mobi(book, savePath);
            }
            MessageBox.Show("下载完成,文件保存在:" + savePath);
            if (checkBox1.Checked)
            {
                try
                {
                    KindleSender.Service.Program.Main();
                    Configuration config = new Configuration()
                    {
                        SmtpServer   = textBox4.Text,
                        SmtpPort     = 25,
                        SmtpPassword = textBox3.Text,
                        SmtpUserName = textBox5.Text,
                        KindleMail   = textBox1.Text,
                        FolderPath   = savePath.Replace(Path.GetFileName(savePath), string.Empty)
                    };
                    FileSender sen = new FileSender(config);
                    sen.Send(savePath);
                    MessageBox.Show("已完成!");
                }
                catch (Exception ee)
                {
                    loglibrary.LogHelper.Error(ee);
                    loglibrary.LogHelper.Flush();
                    MessageBox.Show("错误!\n 错误信息为:" + ee.Message);
                }
            }
        }
Exemple #3
0
        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);
        }