Example #1
0
        public static DelayedCall StartAsync(Callback callback, int interval)
        {
            DelayedCall result = CreateAsync(callback, interval);

            if (interval > 0)
            {
                result.Start();
            }
            else if (interval == 0)
            {
                result.FireNow();
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// PrepareDelayedCallObject method.
        /// </summary>
        /// <param name="delayedCall">DelayedCall instance.</param>
        /// <param name="interval">Timer interval in milliseconds.</param>
        /// <param name="async">Whether use async or not.</param>
        protected static void PrepareDelayedCallObject(DelayedCall delayedCall, int interval, bool async)
        {
            if (interval < 0)
            {
                throw new ArgumentOutOfRangeException("interval", "The new timeout must be zero or greater than zero.");
            }

            delayedCall.SyncContext = null;

            if (!async)
            {
                delayedCall.SyncContext = SynchronizationContext.Current;

                if (delayedCall.SyncContext == null)
                {
                    throw new InvalidOperationException("Cannot delay calls synchronously on a non-UI thread. Use the *Async methods instead.");
                }
            }

            if (delayedCall.SyncContext == null)
            {
                delayedCall.SyncContext = new SynchronizationContext();
            }

            delayedCall._timer = new System.Timers.Timer();

            if (interval > 0)
            {
                delayedCall._timer.Interval = interval;
            }

            delayedCall._timer.AutoReset = false;
            delayedCall._timer.Elapsed  += delayedCall.TimerElapsed;

            Register(delayedCall);
        }