Пример #1
0
        private void Init(ILogger logger, Func <object, Task> asynCallback, TimerCallback synCallback, object state, TimeSpan due, TimeSpan period)
        {
            if (synCallback == null && asynCallback == null)
            {
                throw new ArgumentNullException("synCallback", "Cannot use null for both sync and asyncTask timer callbacks.");
            }
            int numNonNulls = (asynCallback != null ? 1 : 0) + (synCallback != null ? 1 : 0);

            if (numNonNulls > 1)
            {
                throw new ArgumentNullException("synCallback", "Cannot define more than one timer callbacks. Pick one.");
            }
            if (period == TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("period", period, "Cannot use TimeSpan.Zero for timer period");
            }

            this.asyncTaskCallback = asynCallback;
            syncCallbackFunc       = synCallback;
            timerFrequency         = period;
            this.dueTime           = due;
            totalNumTicks          = 0;
            this.logger            = logger;
            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.Debug(ErrorCode.TimerChanging, "Creating timer {0} with dueTime={1} period={2}", GetFullName(), due, period);
            }

            timer = NonCapturingTimer.Create(HandleTimerCallback, state, Constants.INFINITE_TIMESPAN, Constants.INFINITE_TIMESPAN);
        }
Пример #2
0
        private void Init(ILogger logger, Func <object, Task> asynCallback, TimerCallback synCallback, object state, TimeSpan due, TimeSpan period)
        {
            if (synCallback == null && asynCallback == null)
            {
                throw new ArgumentNullException("synCallback", "Cannot use null for both sync and asyncTask timer callbacks.");
            }
            int numNonNulls = (asynCallback != null ? 1 : 0) + (synCallback != null ? 1 : 0);

            if (numNonNulls > 1)
            {
                throw new ArgumentNullException("synCallback", "Cannot define more than one timer callbacks. Pick one.");
            }
            if (period == TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("period", period, "Cannot use TimeSpan.Zero for timer period");
            }

            long dueTm = (long)dueTime.TotalMilliseconds;

            if (dueTm < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(dueTime), "The due time must not be less than -1.");
            }
            if (dueTm > MaxSupportedTimeout)
            {
                throw new ArgumentOutOfRangeException(nameof(dueTime), "The due time interval must be less than 2^32-2.");
            }

            long periodTm = (long)period.TotalMilliseconds;

            if (periodTm < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The period must not be less than -1.");
            }
            if (periodTm > MaxSupportedTimeout)
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The period interval must be less than 2^32-2.");
            }

            this.asyncTaskCallback = asynCallback;
            syncCallbackFunc       = synCallback;
            timerFrequency         = period;
            this.dueTime           = due;
            totalNumTicks          = 0;
            this.logger            = logger;
            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.LogDebug((int)ErrorCode.TimerChanging, "Creating timer {Name} with dueTime={DueTime} period={period}", GetFullName(), due, period);
            }

            timer = NonCapturingTimer.Create(HandleTimerCallback, state, Constants.INFINITE_TIMESPAN, Constants.INFINITE_TIMESPAN);
        }