Example #1
0
 public InitTimerEvent(MachineId client, TimerId tid, bool IsPeriodic, int period)
 {
     this.client     = client;
     this.IsPeriodic = IsPeriodic;
     this.Period     = period;
     this.tid        = tid;
 }
Example #2
0
        /// <summary>
        /// Start a timer.
        /// </summary>
        /// <param name="payload">Payload of the timeout event.</param>
        /// <param name="IsPeriodic">Specifies whether a periodic timer is desired.</param>
        /// <param name="period">Periodicity of the timeout events in ms.</param>
        /// <returns>The id of the created timer.</returns>
        protected TimerId StartTimer(object payload, int period, bool IsPeriodic)
        {
            // The specified period must be valid
            this.Assert(period >= 0, "Timer period must be non-negative");

            var mid = this.Runtime.CreateMachineId(this.Runtime.GetTimerMachineType());
            var tid = new TimerId(mid, payload);

            this.Runtime.CreateMachine(mid, this.Runtime.GetTimerMachineType(), new InitTimerEvent(this.Id, tid, IsPeriodic, period));

            this.TimerIds.Add(tid);
            return(tid);
        }
Example #3
0
        private void InitializeTimer()
        {
            var e = (this.ReceivedEvent as InitTimerEvent);

            this.Client     = e.client;
            this.IsPeriodic = e.IsPeriodic;
            this.Period     = e.Period;
            this.tid        = e.tid;

            this.IsTimerEnabled = true;
            this.timer          = new System.Timers.Timer(Period);

            if (!IsPeriodic)
            {
                this.timer.AutoReset = false;
            }

            this.timer.Elapsed += ElapsedEventHandler;
            this.timer.Start();
        }
Example #4
0
        /// <summary>
        /// Stop the timer.
        /// </summary>
        /// <param name="timer">Id of the timer machine which is being stopped.</param>
        /// <param name="flush">Clear the queue of all timeout events generated by "timer".</param>
        protected async Task StopTimer(TimerId timer, bool flush = true)
        {
            // Check if the user is indeed trying to halt a valid timer
            this.Assert(this.TimerIds.Contains(timer), "Illegal timer-id given to StopTimer");
            this.TimerIds.Remove(timer);

            this.Send(timer.mid, new HaltTimerEvent(this.Id, flush));

            // Flush the buffer: the timer being stopped sends a markup event to the inbox of this machine.
            // Keep dequeuing eTimeout events (with payload being the timer being stopped), until we see the markup event.
            if (flush)
            {
                while (true)
                {
                    var ev = await this.Receive(Tuple.Create(typeof(TimeoutFlushEvent), new Func <Event, bool>(e => true)),
                                                Tuple.Create(typeof(TimerElapsedEvent), new Func <Event, bool>(e => (e as TimerElapsedEvent).Tid == timer)));

                    if (ev is TimeoutFlushEvent)
                    {
                        break;
                    }
                }
            }
        }
Example #5
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="tid">Tid</param>
 public TimerElapsedEvent(TimerId tid)
 {
     this.Tid = tid;
 }