예제 #1
0
        //task
        private void Browser_StateChanged(object sender, CefSharp.LoadingStateChangedEventArgs e)
        {
            if (e.IsLoading == false)
            {
                new Thread(
                    () =>
                {
                    var w        = sender as CustomChrominWebBrowser;
                    var infoTask = w.Info;
                    //获取对应View
                    var _TaskView = GetTaskViewModel(w);
                    if (_TaskView.Chrome == null)
                    {
                        _TaskView.Chrome = w;
                    }

                    if (infoTask != null)
                    {
                        LogHelper.Debug("开始执行任务 :" + infoTask.Action);
                        //设置显示标题
                        App.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            _TaskView.Url   = w.Address;
                            _TaskView.Title = w.Title;
                        }));
                        //获取本地Tasksize
                        //if (_TaskView.TotalCount < w.TaskSize()) _TaskView.TotalCount = w.TaskSize();

                        //JQ环境检查与注入
                        var _jq = JSHelper.EnableJQ(e.Browser.MainFrame);
                        _jq.Wait();
                        if (!_jq.Result)
                        {
                            JSHelper.InjuctJQ(e.Browser.MainFrame);
                        }


                        string url = w.Info.Action;   //即使页面发生跳转也按指定的task进行解析
                                                      //App.Current.Dispatcher.Invoke((Func<string>)(() => { return w.Address; }));

                        //进行解析
                        var _result = ExcuteAnalysis(w, e.Browser.MainFrame, CmdHelper.GetRule(url), infoTask, _TaskView);
                        _result.Wait();
                        Console.WriteLine("Task 执行完毕 出错值为" + _result.Result);
                        w.Info = null;

                        App.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            //只获取当前存储路径
                            _TaskView.Path = infoTask.WorkDirectory;

                            if (_result.Result > 0)
                            {
                                _TaskView.ErrorCount += 1;
                            }
                            else
                            {
                                _TaskView.SuccessCount += 1;
                            }
                        }));
                    }
                    //控制响应
                    if (_TaskView.IsPauseing)
                    {
                        _TaskView.IsPaused   = true;
                        _TaskView.IsPauseing = false;
                        LogHelper.Debug(w.ID + " 任务已被用户主动暂停");
                        return;
                    }

                    if (_TaskView.IsStopping)
                    {
                        _TaskView.IsStopped  = true;
                        _TaskView.IsStopping = false;
                        LogHelper.Debug(w.ID + " 任务已被用户主动停止");
                        return;
                    }


                    if (w.TaskSize() > 0)
                    {
                        //if (_TaskView.TotalCount < w.TaskSize()) _TaskView.TotalCount = w.TaskSize();
                        //获取下一个解析任务
                        w.Info = w.PopTask();

                        //跳转网页地址
                        App.Current.Dispatcher.Invoke(
                            (Action)(() =>
                        {
                            w.Address = w.Info.Action;
                        }));

                        LogHelper.Debug(w.ID + " 已分配下一个内部任务 " + w.Info.Action);
                    }
                    else
                    {
                        App.Current.Dispatcher.BeginInvoke(new Action(() => {
                            _TaskView.IsCompleted = true;
                        }));
                        //Console.WriteLine("Finished ===> {0}", _TaskView.IsCompleted);


                        //重置以回收
                        //w.Reset();
                        BH.Reuse(w);
                        RemoveTaskBrowserView(w);
                        UpdateTasekViewUI();
                        LogHelper.Debug(w.ID + " 的内部任务已执行完毕,并已重置后回收到对象池");

                        //若向主任务队列中加入了子任务,而主任务分配线程已结束,需要重新启动分配线程
                        if (TaskHelper.HasTask() && !BK.IsBusy)
                        {
                            BK.RunWorkerAsync();
                        }
                    }
                }
                    ).Start();
            }
        }