Example #1
0
        /// <summary>
        /// 移除一个任务
        /// </summary>
        /// <param name="task"></param>
        protected void RemoveTaskRunner(WlyTaskRunner task)
        {
            if (m_mainList.Remove(task))
            {
                InternalInitTasks();
            }
            else
            {
                m_dailyList.Remove(task);
            }

            if (Application.Current != null)
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() => { TaskList.Remove(task); }));
            }
        }
Example #2
0
        /// <summary>
        /// 挂机线程
        /// </summary>
        private void AutoPlayThread()
        {
            m_run       = true;
            m_final     = true;
            m_isDevelop = false;
            var errorCount = 0;

            // 最外层死循环,进程守护
            while (m_run)
            {
                try
                {
                    // 登录
                    OnDescriptionChanged($"开始登录 {Account}");
                    DMGuid = string.Empty;
                    WlyUtilityBiz.Login(this);

                    // 绑定游戏窗口
                    BindGameWnd();
                    InternalAfterLogin();

                    // 登录成功,等待登录完成
                    OnDescriptionChanged("等待加载完成");
                    WlyViewMgr.GoTo(DMGuid, WlyViewType.场景_主界面, TimeSpan.FromSeconds(2));

                    // 开始挂机
                    m_aliveTime = DateTime.Now;
                    while (m_run)
                    {
                        if (DMService.Instance.GetWindowState(DMGuid, WndHwnd, 0) == 0)
                        {
                            WxLog.Error($"WlyEntity.AutoPlayThread  游戏窗口不存在");
                            break;
                        }

                        WlyTaskRunner runner = null;

                        // 检测是否有可以运行的日常任务
                        if (m_dailyList.Any() && (m_dailyList.First().StartTime < DateTime.Now))
                        {
                            runner = m_dailyList.First();
                            OnDescriptionChanged(runner.Task.MainTitle);
                            CurrentRunner = runner;
                            var info = runner.Task.Run(this);
                            WxLog.Debug($"WlyEntity.AutoPlayThread Name <{this}> Run <{runner.Task}> Result <{info}>");

                            // 无论结果,先进行保存
                            SaveTaskInfo(info);
                            RemoveTaskRunner(runner);
                            if (!info.IsComplete)
                            {
                                // 未完成的任务重新加入,等待下一次运行
                                AddTask(runner.Task, info.NextRunTime);
                            }

                            m_aliveTime = DateTime.Now;
                            continue;
                        }

                        // 检测是否有可以运行的主线任务
                        runner = m_mainList.FirstOrDefault(o => o.StartTime < DateTime.Now);
                        if (runner != null)
                        {
                            OnDescriptionChanged(runner.Task.MainTitle);
                            CurrentRunner = runner;
                            var info = runner.Task.Run(this);

                            // 无论结果,先进行保存
                            SaveTaskInfo(info);
                            RemoveTaskRunner(runner);
                            if (!info.IsComplete)
                            {
                                // 未完成的任务重新加入,等待下一次运行
                                AddTask(runner.Task, info.NextRunTime);
                            }

                            m_aliveTime = DateTime.Now;
                            continue;
                        }

                        CurrentRunner = null;

                        // 运行空闲任务
                        InternalBackupWorkThread();

                        // 超时重启
                        if (DateTime.Now - m_aliveTime > m_restartInterval)
                        {
                            break;
                        }
                    }

                    while (m_finalList.Any(o => o.StartTime < DateTime.Now))
                    {
                        var runner = m_finalList.FirstOrDefault(o => o.StartTime < DateTime.Now);
                        if (runner == null)
                        {
                            break;
                        }

                        OnDescriptionChanged(runner.Task.MainTitle);
                        CurrentRunner = runner;
                        var info = runner.Task.Run(this);
                        WxLog.Debug($"WlyEntity.AutoPlayThread Name <{this}> Run <{runner.Task}> Result <{info}>");

                        // 无论结果,先进行保存
                        SaveTaskInfo(info);
                        m_finalList.Remove(runner);
                        if (!info.IsComplete)
                        {
                            // 未完成的任务重新加入,等待下一次运行
                            AddTask(runner.Task, info.NextRunTime);
                        }

                        m_aliveTime = DateTime.Now;
                    }

                    m_final = false;
                }
                catch (InvalidOperationException ex)
                {
                    WxLog.Error($"WlyEntity.InternalBackupWorkThread Error {ex}");
                    errorCount++;
                    InternalHandleError(errorCount);

                    //CaptureError(ex);
                }
                catch (Exception ex)
                {
                    WxLog.Error($"WlyEntity.AutoPlayThread Error <{ex}>");
                }
                finally
                {
                    CloseProcess();
                }
            }

            WxLog.Debug($"WlyEntity.AutoPlayThread Stopped, Hwnd <{WndHwnd}>, Run <{Run}>");
            OnStopped();
        }