示例#1
0
        private static void InvokeActions(bool isChildren, int taskID, List <ActionModel> actions, CancellationToken ct, int index = 0)
        {
            if (actions == null || actions.Count == 0)
            {
                return;
            }
            ct.ThrowIfCancellationRequested();
            var actionModel = actions[index];
            var action      = GetAction(actionModel);

            action.OnEventStateChanged += (taskID, actionID, state) =>
            {
                if (ct.IsCancellationRequested)
                {
                    OnActionState?.Invoke(taskID, actionID, Types.ActionInvokeStateType.Done);
                    ct.ThrowIfCancellationRequested();
                }
                if (state == ActionInvokeStateType.Done)
                {
                    OnActionState?.Invoke(taskID, actionID, Types.ActionInvokeStateType.Done);
                    if (index + 1 < actions.Count)
                    {
                        InvokeActions(isChildren, taskID, actions, ct, index + 1);
                    }
                    else if (!isChildren)
                    {
                        //全部运行完成
                        Debug.WriteLine("全部运行完成");
                        OnActionGroupState?.Invoke(taskID, ActionInvokeStateType.Done);
                    }
                }
            };
            OnActionState?.Invoke(taskID, actionModel.ID, Types.ActionInvokeStateType.Runing);
            action.GenerateAction(taskID, actionModel)?.Invoke();
        }
示例#2
0
 public static void StopTestInvokeAction()
 {
     OnActionGroupState?.Invoke(TestTaskID, ActionInvokeStateType.Busy);
     if (testcts.IsCancellationRequested)
     {
         testcts?.Cancel();
     }
 }
示例#3
0
        public static void Invoke(int taskID, List <ActionModel> actions, bool isTest = false, bool isChildren = false)
        {
            if (actions == null)
            {
                return;
            }
            CancellationTokenSource cts;

            if (isTest)
            {
                cts = testcts;
            }
            else
            {
                cts = new CancellationTokenSource();
            }

            CancellationToken ct = cts.Token;


            if (isChildren)
            {
                InvokeActions(isChildren, taskID, actions, ct);
            }
            else
            {
                var task = new Task(() =>
                {
                    try
                    {
                        OnActionGroupState?.Invoke(taskID, ActionInvokeStateType.Runing);
                        InvokeActions(isChildren, taskID, actions, ct);
                    }
                    catch (OperationCanceledException)
                    {
                        OnActionGroupState?.Invoke(taskID, ActionInvokeStateType.Done);
                    }
                    catch (Exception e)
                    {
                        LogHelper.Error(e.ToString());
                    }
                    //finally
                    //{
                    //    cts.Dispose();
                    //}
                }, ct);

                task.Start();
            }
        }