コード例 #1
0
        /// <summary>
        /// 从数据库获取数据
        /// <param name="catalog"></param>
        /// <param name="isBackGround"></param>
        /// <returns></returns>
        private async Task <string> GetCatafromDatabase(BookCatalog catalog, bool isBackGround = true)
        {
            string html = null;
            await Task.Run(() =>
            {
                try
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        IsLoading = true;
                    });

                    var item = this.BookEntity.CatalogList.FirstOrDefault(p => p.CatalogUrl == catalog.CatalogUrl);
                    if (item != null)
                    {
                        var content = DBBookCatalogContent.SelectBookCatalogContent(AppDataPath.GetBookDBPath(BookEntity.BookID), item.CatalogUrl);
                        if (content != null)
                        {
                            html = content.Content;
                        }
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        IsLoading = false;
                    });
                }
            });

            return(html);
        }
コード例 #2
0
        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;
                });
            });
        }
コード例 #3
0
        public async void StartNewFastDownLoadInstance(DowmLoadEntity temp)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            IsDownLoading = true;

            int count = 10;

            temp.IsFast = true;

            if (temp.Entity.CatalogList == null || temp.Entity.CatalogList.Count == 0)
            {
                return;
            }
            temp.ContentList = new List <BookCatalogContent>();

            count = temp.Entity.CatalogList.Count >= count ? count : temp.Entity.CatalogList.Count;
            Task[] tasks = new Task[count];

            var groups = Split <BookCatalog>(temp.Entity.CatalogList, count);
            int index  = 0;

            if (!string.IsNullOrEmpty(temp.Entity.Cover))
            {
                try
                {
                    StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(AppDataPath.GetLocalBookCoverFolderPath());

                    StorageFile file     = null;
                    string      filename = temp.Entity.BookID + ".jpg";
                    if (!File.Exists(filename))
                    {
                        file = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                    }
                    file = await storageFolder.GetFileAsync(filename);

                    using (var stream = await file.OpenReadAsync())
                    {
                        if (stream.Size <= 0)
                        {
                            var tmpfile = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                            var http = new HttpClient();
                            var data = await http.GetByteArrayAsync(temp.Entity.Cover);

                            await FileIO.WriteBytesAsync(tmpfile, data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(temp.Entity.BookName + "  下载封面失败");
                }
            }

            try
            {
                foreach (var list in groups)
                {
                    tasks[index] = await Task.Factory.StartNew(async() =>
                    {
                        HttpHelper http = new HttpHelper();
                        foreach (var catalog in list)
                        {
                            if (temp.IsPause)
                            {
                                return;
                            }
                            if (string.IsNullOrEmpty(catalog.CatalogUrl))
                            {
                                continue;
                            }
                            try
                            {
                                string html = await GetHtmlData(catalog.CatalogUrl, http);

                                if (string.IsNullOrEmpty(html))
                                {
                                    html = GetHtmlData(catalog.CatalogUrl, http).Result;
                                }
                                if (string.IsNullOrEmpty(html))
                                {
                                    Debug.WriteLine("*******下载失败****** :" + temp.Entity.BookName + "  " + catalog.CatalogName + "   " + catalog.CatalogUrl);
                                }
                                else
                                {
                                    Debug.WriteLine("下载完成 :" + temp.Entity.BookName + "  " + catalog.CatalogName + "   " + catalog.CatalogUrl);
                                }

                                BookCatalogContent content = new BookCatalogContent()
                                {
                                    BookID     = catalog.BookID,
                                    CatalogUrl = catalog.CatalogUrl,
                                    Content    = html,
                                };
                                temp.ContentList.Add(content);


                                if (IsFrameContent)
                                {
                                    DispatcherHelper.CheckBeginInvokeOnUI(temp.SetProcessValue);

                                    //await Task.Factory.StartNew((obj) =>
                                    //{
                                    //    temp.SetProcessValue();
                                    //}, null, new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler);
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                                continue;
                            }
                        }
                    });

                    index++;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                temp.IsPause = true;
            }


            await Task.Factory.ContinueWhenAll(tasks, (obj) =>
            {
                if (temp.IsPause)
                {
                    if (DownLoadList.Contains(temp))
                    {
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            this.DownLoadList.Remove(temp);
                        });
                    }

                    if (File.Exists(AppDataPath.GetLocalBookCoverPath(temp.Entity.BookID)))
                    {
                        File.Delete(AppDataPath.GetLocalBookCoverPath(temp.Entity.BookID));
                    }
                    return;
                }
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    temp.SetProcessValue();
                    temp.Entity.LastReadChapterName = null;
                    temp.Entity.LastReadChapterUrl  = null;
                    temp.Entity.IsLocal             = true;

                    var catalog = temp.Entity.CatalogList.OrderBy(p => p.Index).ToList().LastOrDefault();
                    if (catalog != null)
                    {
                        temp.Entity.NewestChapterName = catalog.CatalogName;
                        temp.Entity.NewestChapterUrl  = catalog.CatalogUrl;
                    }
                });

                DBBookCatalogContent.InsertOrUpdateBookCatalogContents(AppDataPath.GetBookDBPath(temp.Entity.BookID), temp.ContentList);
                DBBookCatalog.InsertOrUpdateBookCatalogs(AppDataPath.GetBookDBPath(temp.Entity.BookID), temp.Entity.CatalogList.ToList());
                lock (isAdd1)
                {
                    DBLocalBook.InsertOrUpdateBookEntity(AppDataPath.GetLocalBookDBPath(), temp.Entity);
                }

                try
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        this.DownLoadList.Remove(temp);
                        ViewModelInstance.Instance.LocalBookPage.LocalBookList.Add(temp.Entity);
                        ToastHeplper.ShowMessage(temp.Entity.BookName + "下载完毕,点击“本地图书”查看");
                    });
                }
                catch (Exception ex)
                {
                    this.DownLoadList.Remove(temp);
                    ToastHeplper.ShowMessage(temp.Entity.BookName + "下载失败");
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    if (this.DownLoadList.Count == 0)
                    {
                        this.IsDownLoading = false;
                    }
                }

                watch.Stop();
                Debug.WriteLine("共用时:" + watch.Elapsed.TotalSeconds);
            });
        }
コード例 #4
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 + "下载已暂停");
                            }
                        });
                    }
                }
            });
        }