示例#1
0
        private CancellableAction DequeueAction()
        {
            CancellableAction cancellableAction = actions[0];

            actions.RemoveAt(0);
            return(cancellableAction);
        }
示例#2
0
        public IDisposable ScheduleTask(TimeSpan timeSpan, Action action)
        {
            CancellableAction cancellableAction = new CancellableAction(action);

            queue.Enqueue(CurrentTime + timeSpan, cancellableAction);
            return(new Disposable(() => cancellableAction.Cancel()));
        }
示例#3
0
        public static ActionTask Run(CancellableAction doThis, CancellationToken cancellationToken = null, TaskScheduler scheduler = null)
        {
            var task = new ActionTask(doThis, cancellationToken, scheduler);

            task.Start();
            return(task);
        }
示例#4
0
 /// <summary>
 /// Creates an unstarted Task.
 /// </summary>
 /// <param name="doThis"></param>
 /// <param name="cancellationToken"></param>
 /// <param name="scheduler"></param>
 internal ActionTask(CancellableAction doThis, CancellationToken cancellationToken = null, TaskScheduler scheduler = null)
     : base(cancellationToken, scheduler)
 {
     if (doThis == null)
     {
         throw new ArgumentNullException("doThis");
     }
     _userWork = doThis;
 }
示例#5
0
 /// <summary>
 /// Creates an unstarted Task.
 /// </summary>
 /// <param name="doThis"></param>
 /// <param name="scheduler"></param>
 internal ActionTask(Action doThis, TaskScheduler scheduler = null)
     : base(CancellationToken.None, scheduler)
 {
     if (doThis == null)
     {
         throw new ArgumentNullException("doThis");
     }
     _userWork = token => doThis();
 }
示例#6
0
 /// <summary>
 /// Enforces execution of particular method. It will be retried on each garbage collect.
 /// </summary>
 /// <param name="action"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 private void ShouldExecute(CancellableAction action, CancellationToken token)
 {
     try
     {
         action(token);
         return;
     }
     catch { }
     _actions.Add(action);
 }
示例#7
0
        internal static ActionTask BuildContinuation(Task antecedent,
                                                     CancellableAction doThis,
                                                     CancellationToken cancellationToken,
                                                     TaskScheduler scheduler = null)
        {
            var continuation = new ActionTask(doThis, cancellationToken, scheduler);

            continuation.SetStatusToUnstartedContinuation();
            TaskContinuationManager.RegisterContinuation(antecedent, continuation);
            return(continuation);
        }
示例#8
0
        /// <summary>
        /// Enforces execution of particular method. It will be retried on each garbage collect.
        /// </summary>
        /// <param name="action"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private async Task ShouldExecute(CancellableAction action, CancellationToken token)
        {
            try
            {
                await action(token);

                return;
            }
            catch { }
            _actions.Add(action);
        }
        /// <inheritdoc />
        public AsyncCancellableCommand(CancellableAction <object?> action, Predicate <object?> canExecute) : base(canExecute)
        {
            if (action is null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            AllowMultipleExecutions = false;
            _action         = action;
            CommandStarted += (sender, args) => _cancelSource = new CancellationTokenSource();
            _cancelSource   = new CancellationTokenSource();
        }
示例#10
0
        public IDisposable ScheduleTask(TimeSpan timeSpan, Action action)
        {
            CancellableAction cancellableAction = new CancellableAction(CurrentTime + timeSpan, action);

            EnqueueAction(cancellableAction);

            if (timeSpan == TimeSpan.Zero && !isProcessing && !isImmediateProcessingDisabled)
            {
                ProcessDueOperations();
            }

            return(new Disposable(() => cancellableAction.Cancel()));
        }
示例#11
0
 private CancellableAction <T> Wrap(CancellableAction <T> act)
 {
     return((task, _token, _progress) => {
         if (_progress.HasFinished) //was it cancelled prior to calling.
         {
             return;
         }
         act(task, _token, _progress);
         if (_progress.HasFinished == false)
         {
             _progress.SetAsFinished();
         }
     });
 }
示例#12
0
        private void EnqueueAction(CancellableAction cancellableAction)
        {
            int i = 0;

            foreach (CancellableAction action in actions)
            {
                if (cancellableAction.TimeSpan < action.TimeSpan)
                {
                    break;
                }

                i++;
            }

            actions.Insert(i, cancellableAction);
        }
示例#13
0
文件: QueueThread.cs 项目: Nucs/nlib
 public QueuedTask <T> QueueTask(CancellableAction <T> act)
 {
     if (_threadstop)
     {
         return(null);
     }
     lock (Queue) {
         if (_threadstop)
         {
             return(null);
         }
         var pd     = new ProgressDesciber();
         var queued = new QueuedTask <T>(act);
         Queue.Enqueue(queued);
         queued.State = TaskState.Queued;
         FireTaskQueued(queued);
         res.Set();
         return(queued);
     }
 }
示例#14
0
 public QueuedTask(CancellableAction <T> action)
 {
     Action   = Wrap(action);
     Progress = new ProgressDesciber();
     Token    = Progress.Token;
 }
示例#15
0
 public static ActionTask New(CancellableAction doThis, CancellationToken cancellationToken = null, TaskScheduler scheduler = null)
 {
     return(new ActionTask(doThis, cancellationToken, scheduler));
 }
示例#16
0
 /// <inheritdoc />
 public AsyncCancellableCommand(CancellableAction action) : this(action, AlwaysExecute)
 {
 }
示例#17
0
 /// <inheritdoc />
 public AsyncCancellableCommand(CancellableAction action, Predicate <object?> canExecute) : this((ct, _) => action(ct), canExecute)
 {
 }
示例#18
0
 public QueuedTask(CancellableAction <T> action, ProgressDesciber progress, CancellationToken token)
 {
     Action   = Wrap(action);
     Progress = progress;
     Token    = token;
 }
示例#19
0
 protected override void ExecuteUserWork(CancellationToken cancellationToken)
 {
     _userWork(cancellationToken);
     _userWork = null;
 }
示例#20
0
 public IDisposable ScheduleTask(TimeSpan timeSpan, Action action)
 {
     CancellableAction cancellableAction = new CancellableAction(action);
     queue.Enqueue(CurrentTime + timeSpan, cancellableAction);
     return new Disposable(() => cancellableAction.Cancel());
 }
示例#21
0
 public static ActionTask Run(CancellableAction doThis, CancellationToken cancellationToken = null)
 {
     return(Task.Run(doThis, cancellationToken, TaskScheduler.DefaultScheduler));
 }