Example #1
0
        /// <summary>
        /// 将指定的url转换成为
        /// </summary>
        /// <param name="indexPageUrl">列表页面</param>
        /// <param name="ContentPageUrls">内容页面列表</param>
        /// <returns></returns>
        List <XMLDocuentAnalyseEntity> ParsPageUrlsToXDocuments(ListPageContentUrl indexPageUrl, System.Collections.Generic.IEnumerable <ListPageContentUrl> ContentPageUrls)
        {
            List <XMLDocuentAnalyseEntity> result = new List <XMLDocuentAnalyseEntity>();

            //需要分析的url
            System.Collections.Generic.List <ListPageContentUrl> analyseUrls = new List <ListPageContentUrl>();

            //如果转url大于10则只取10个
            analyseUrls = ContentPageUrls.Take(10).ToList();

            //开始创建多个并行任务数组
            System.Threading.Tasks.Task[] tasks = new System.Threading.Tasks.Task <XMLDocuentAnalyseEntity> [analyseUrls.Count];
            //实例化数组
            for (int k = 0; k < tasks.Length; k++)
            {
                tasks[k] = System.Threading.Tasks.Task.Factory.StartNew <XMLDocuentAnalyseEntity>((url) =>
                {
                    return(FillContentUrl(indexPageUrl, (ListPageContentUrl)url));
                }, analyseUrls[k]);
            }

            //等待并行计算完成
            System.Threading.Tasks.Task.WaitAll(tasks);

            //填充结果
            tasks.ToList().ForEach((o) =>
            {
                if (o.IsCompleted)
                {
                    result.Add(((System.Threading.Tasks.Task <XMLDocuentAnalyseEntity>)o).Result);
                }
                //表示异步已经处理
                if (o.Exception != null)
                {
                    o.Exception.Handle((ex) =>
                    {
                        System.Diagnostics.UDPGroup.SendStrGB2312(ex.Message + "|||||" + ex.StackTrace);
                        return(true);
                    });
                }
                if (o.Status == System.Threading.Tasks.TaskStatus.RanToCompletion ||
                    o.Status == System.Threading.Tasks.TaskStatus.Faulted ||
                    o.Status == System.Threading.Tasks.TaskStatus.Canceled)
                {//释放资源
                    o.Dispose();
                }
            });
            analyseUrls.Clear();

            return(result);
        }