Exemplo n.º 1
0
            public FixedRateDelayTask(ThreadTimerExecutor executor, Action command, TimeSpan initialDelay,
                                      TimeSpan period) : base()
            {
                this._startTime    = DateTime.Now.Ticks;
                this._initialDelay = initialDelay;
                this._period       = period;
                this._executor     = executor;

                this._wrappedAction = () =>
                {
                    try
                    {
                        if (this.IsDone)
                        {
                            return;
                        }

                        if (this.IsCancellationRequested)
                        {
                            this.SetCancelled();
                        }
                        else
                        {
                            Interlocked.Increment(ref _count);
                            this._executor.Add(this);
                            command();
                        }
                    }
                    catch (Exception)
                    {
                    }
                };
                this._executor.Add(this);
            }
Exemplo n.º 2
0
            public OneTimeDelayTask(ThreadTimerExecutor executor, Action command, TimeSpan delay) : base(true)
            {
                this._startTime     = DateTime.Now.Ticks;
                this._delay         = delay;
                this._executor      = executor;
                this._wrappedAction = () =>
                {
                    try
                    {
                        if (this.IsDone)
                        {
                            return;
                        }

                        if (this.IsCancellationRequested)
                        {
                            this.SetCancelled();
                        }
                        else
                        {
                            command();
                            this.SetResult();
                        }
                    }
                    catch (Exception e)
                    {
                        this.SetException(e);
                    }
                };
                this._executor.Add(this);
            }
Exemplo n.º 3
0
            public DurationFixedRateTask(ThreadTimerExecutor executor, Action <long> fixedUpdateCommand,
                                         Action endCommand, TimeSpan?initialDelay, TimeSpan?period, TimeSpan?duration) : base()
            {
                this._startTime    = DateTime.Now.Ticks;
                this._initialDelay = initialDelay ?? TimeSpan.Zero;
                this._period       = period ?? TimeSpan.FromMilliseconds(10);
                this._executor     = executor;
                this._endCommand   = endCommand;
                this._executor.Add(this);

                this._wrappedAction = () =>
                {
                    try
                    {
                        if (this.IsDone)
                        {
                            return;
                        }

                        if (this.IsCancellationRequested)
                        {
                            this.SetCancelled();
                        }
                        else
                        {
                            if (_count >= 1)
                            {
                                _timer += _period;
                            }
                            if (duration.HasValue && _timer >= duration)
                            {
                                Cancel();
                            }
                            fixedUpdateCommand((long)_timer.TotalMilliseconds);
                            this._executor.Add(this);
                            Interlocked.Increment(ref _count);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e);
                    }
                };
            }