Esempio n. 1
0
        /// <summary>
        /// 构建索引
        /// </summary>
        public void Build()
        {
            TableIndexProcessor processor = new TableIndexProcessor();
            var taskDataList = processor.GetEachThreadTask(40);

            TaskFactory taskFactory = new TaskFactory();

            taskFactory.StartNew(() =>
            {
                List <Task> taskList = new List <Task>();
                var threadNums       = Enumerable.Range(1, StaticConst.CategorySheetCount).ToList();
                Random random        = new Random();
                int index            = 0, alltaskCount = taskDataList.Count;
                foreach (var taskData in taskDataList)
                {
                    index++;
                    var threadCode = CommodityDAL.GetTName(random.Next(1, threadNums.Count));
                    Task task      = taskFactory.StartNew(() =>
                    {
                        try
                        {
                            LuceneBulid luceneBuild = new LuceneBulid();
                            if (!PathSuffixList.Any(u => u == threadCode))
                            {
                                PathSuffixList.Add(threadCode);
                            }
                            luceneBuild.BuildIndex(taskData, threadCode, true);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"BuildIndexError\t{ex.Message}");
                            StringValueEventArgs e = new StringValueEventArgs()
                            {
                                Value = ex.Message
                            };
                            OnTaskError(e);//每完成一个任务触发事件
                            CTS.Cancel();
                        }
                    }, CTS.Token).ContinueWith(t =>
                    {
                        ValueEventArgs e = new ValueEventArgs()
                        {
                            Value = (int)(index * 1.0 / alltaskCount) * 100
                        };
                        OnValueChanged(e);//每完成一个任务触发事件
                    });
                    taskList.Add(task);
                    if (taskList.Count > 20)
                    {
                        taskList = taskList.Where(t => !t.IsCompleted && !t.IsCanceled && !t.IsFaulted).ToList();
                        Task.WaitAny(taskList.ToArray());
                    }
                }
                taskList.Add(taskFactory.ContinueWhenAll(taskList.ToArray(), MergeAllLuceneIndex));
            });
        }
Esempio n. 2
0
 private void MergeAllLuceneIndex(Task[] obj)
 {
     try
     {
         ILuceneBulid builder = new LuceneBulid();
         builder.MergeAllLuceneIndex(PathSuffixList.ToArray());
         OnTaskComplate(new EventArgs());//任务完成触发事件
     }
     catch (Exception ex)
     {
         StringValueEventArgs e = new StringValueEventArgs()
         {
             Value = ex.Message
         };
         OnTaskError(e);//每完成一个任务触发事件
         Console.WriteLine($"MergeAllLuceneIndex\t{ex.Message}{ex}");
     }
 }
Esempio n. 3
0
 protected void OnTaskError(StringValueEventArgs e)
 {
     TaskError?.Invoke(this, e);
 }