private async void OnUpdateCommand()
        {
            var reslut = LocalBookList.FirstOrDefault(p => !string.IsNullOrEmpty(p.UnReadCountData));

            if (reslut != null)
            {
                ToastHeplper.ShowMessage("开始更新。");
            }
            else
            {
                ToastHeplper.ShowMessage("无更新,请稍后再试。");
                IsUpdating = false;
                return;
            }

            IsUpdating = true;
            Task[] tasks = new Task[LocalBookList.Count];

            for (int j = 0; j < LocalBookList.Count; j++)
            {
                var item = LocalBookList[j];
                tasks[j] = Task.Run((Action)(async() =>
                {
                    try
                    {
                        if (item.UnDownloadCatalogList == null || item.UnDownloadCatalogList.Count == 0)
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                item.UnReadCountData = "";
                            });
                            return;
                        }
                        var list = new List <BookCatalog>();
                        item.UnDownloadCatalogList.ToList().ForEach(p => list.Add(p));
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (!IsUpdating)
                            {
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    item.UnReadCountData = "  " + item.UnDownloadCatalogList.Count.ToString();
                                });
                                break;
                            }

                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                item.UnReadCountData = "正在更新(" + "剩余" + (list.Count - i).ToString() + ")";
                            });

                            string html = await AnalysisContentService.GetHtmlContent(new HttpHelper(), list[i].CatalogUrl);
                            if (!string.IsNullOrEmpty(html))
                            {
                                BookCatalogContent content = new BookCatalogContent()
                                {
                                    BookID = item.BookID,
                                    CatalogUrl = list[i].CatalogUrl,
                                    Content = html
                                };
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    item.NewestChapterName = list[i].CatalogName;
                                    item.NewestChapterUrl = list[i].CatalogUrl;
                                    item.UpdateTime = DateTime.Now.ToString();

                                    if (item.CatalogList == null)
                                    {
                                        item.CatalogList = new ObservableCollection <BookCatalog>();
                                    }
                                    item.CatalogList.Add(list[i]);
                                    item.UnDownloadCatalogList.Remove(list[i]);

                                    if (i == list.Count - 1)
                                    {
                                        item.UnReadCountData = null;
                                    }
                                });

                                lock (obj)
                                {
                                    DBBookCatalog.InsertOrUpdateBookCatalog(AppDataPath.GetBookDBPath(item.BookID), list[i]);
                                    DBBookCatalogContent.InsertOrUpdateBookCatalogContent(AppDataPath.GetBookDBPath(item.BookID), content);
                                    DBLocalBook.InsertOrUpdateBookEntity(AppDataPath.GetLocalBookDBPath(), item);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }));
            }

            await Task.Factory.ContinueWhenAll(tasks, completedTasks =>
            {
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    IsUpdating = false;
                });
            });
        }
Example #2
0
        public void StartNewCommonDownLoadInstance(DowmLoadEntity temp)
        {
            bool result = false;

            IsDownLoading = true;
            temp.IsFast   = false;

            Task task = Task.Run(async() =>
            {
                try
                {
                    int startIndex = 0;
                    //适用于暂停然后重新开始
                    if (temp.CurrentCatalog != null)
                    {
                        if (temp.Entity.CatalogList != null && temp.Entity.CatalogList.Count > 0)
                        {
                            var catalog = temp.Entity.CatalogList.FirstOrDefault(p => p.CatalogUrl == temp.CurrentCatalog.CatalogUrl);
                            if (catalog != null)
                            {
                                startIndex = temp.Entity.CatalogList.IndexOf(catalog);
                            }
                        }
                    }

                    BookEntity entity = temp.Entity;
                    int count         = temp.Entity.CatalogList.Count;
                    HttpHelper http   = new HttpHelper();
                    for (int i = startIndex; i < count; i++)
                    {
                        if (temp.IsPause)
                        {
                            // break;
                            return;
                        }
                        try
                        {
                            var item = temp.Entity.CatalogList[i];
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                temp.CurrentCatalog = item;
                                temp.CurrentIndex   = i + 1;
                                temp.ProgressValue  = Math.Round(((double)item.Index / (double)temp.Entity.CatalogList.Count), 3) * 100;
                            });

                            string html = await GetHtmlData(item.CatalogUrl, http);

                            BookCatalogContent content = new BookCatalogContent()
                            {
                                BookID     = item.BookID,
                                CatalogUrl = item.CatalogUrl,
                                Content    = html
                            };

                            lock (isAdd1)
                            {
                                if (!string.IsNullOrEmpty(item.CatalogUrl))
                                {
                                    DBBookCatalog.InsertOrUpdateBookCatalog(AppDataPath.GetBookDBPath(temp.Entity.BookID), item);
                                }
                                if (!string.IsNullOrEmpty(content.CatalogUrl))
                                {
                                    DBBookCatalogContent.InsertOrUpdateBookCatalogContent(AppDataPath.GetBookDBPath(temp.Entity.BookID), content);
                                }
                            }

                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                temp.Entity.NewestChapterName = item.CatalogName;
                                temp.Entity.NewestChapterUrl  = item.CatalogUrl;
                            });
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
                finally
                {
                    if (result)
                    {
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            lock (isAdd2)
                            {
                                temp.Entity.LastReadChapterName = null;
                                temp.Entity.LastReadChapterUrl  = null;
                                DBLocalBook.InsertOrUpdateBookEntity(AppDataPath.GetLocalBookDBPath(), temp.Entity);
                            }
                            this.DownLoadList.Remove(temp);
                            if (this.DownLoadList.Count == 0)
                            {
                                this.IsDownLoading = false;
                            }
                            temp.Entity.IsLocal = true;
                            ViewModelInstance.Instance.LocalBookPage.LocalBookList.Add(temp.Entity);
                            ToastHeplper.ShowMessage(temp.Entity.BookName + "下载完毕,点击“本地图书”查看");
                        });
                    }
                    else
                    {
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            if (!temp.IsPause)
                            {
                                this.DownLoadList.Remove(temp);
                                ToastHeplper.ShowMessage(temp.Entity.BookName + "下载失败");
                            }
                            else
                            {
                                //  Services.ToastHeplper.ShowMessage(temp.Entity.BookName + "下载已暂停");
                            }
                        });
                    }
                }
            });
        }