< All Topics

Timer

Experior.Core.Timer is a Timer that expires after a given time. The Experior.Core.Timer is similar to the .Net System.Timers.Timer class but adds Experior specific functionality. The Experior.Core.Timer will pause when the Experior model is paused and will also automatically resume when the Experior model is resumed. In addition it takes into account the Simulated time instead of the world time. So if Experior runs x times faster than the world time than the timer will expire after the given TimeOut divided by x.

The Experior.Core.Timer can be used in PhysX and discrete mode. Note however that is more accurate in discrete mode. (This is because in PhysX mode the time progresses in steps where the delta time is determined by the PhysX engine)

Properties

double TimeRemaining: This property returns the remaining time in seconds of Simulated time before the timer will expire.

double PercentageRemaining: This property returns the percentage (between 0 and 100) of the remaining time in seconds of Simulated time before the timer will expire.  

float Timeout: This property returns the total time in seconds of Simulated time between starting and expiring of the timer.

bool Running: This property returns true when the timer is started.  

bool AutoReset: This property is true when the timer will automatically start again after expiring. When it is false the timer requires a Reset() and Start() to start running again.

Methods

void Start(): This method will start the timer.

void Stop(): This method will stop the timer.

void Reset(): This method will reset the timer. After this the TimeRemaining will equal the TimeOut again. It does not automatically start the timer.

void Dispose(): This method will dispose of the timer.

Static method

If you want to execute a method after a given delay you can use the static Action method as shown here. In that case the action is only executed once (no AutoReset).

Example

Below you find an example where two timers are created. The logtimer changes and prints every second the status of the pickuptimer.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Experior.Core.Assemblies;

namespace Experior.Catalog.Assemblies
{
    public class ExampleTimer:Assembly
    {

        public ExampleTimer(AssemblyInfo info):base(info)
        {
            CreateTimers();
        }

        private int counter = 0;
        private Experior.Core.Timer pickuptimer;
        public void CreateTimers()
        {
            // create a timer that will expire after 10.5 seconds of Simulated Time
            pickuptimer = new Core.Timer(10.5f);
            pickuptimer.AutoReset = false;
            pickuptimer.Elapsed += new Core.Timer.ElapsedEvent(pickuptimer_Elapsed);
            Experior.Core.Timer logtimer = new Core.Timer(1.0f);
            logtimer.AutoReset = true;
            logtimer.Elapsed += new Core.Timer.ElapsedEvent(logtimer_Elapsed);
            logtimer.Start();
            pickuptimer.Start();
        }

        void logtimer_Elapsed(Core.Timer sender)
        {
            counter++;
            Experior.Core.Environment.Log.Write("LogTimer has elapsed " + counter + " times at simulated time " + Experior.Core.Environment.Time.Simulated + " at elapsed time "+ Experior.Core.Environment.Time.Elapsed);
            if (pickuptimer != null)
            {
                Experior.Core.Environment.Log.Write("RemainingTime of pickuptimer " + pickuptimer.TimeRemaining + " percentage remaining" + pickuptimer.PercentageRemaining);
                if (counter == 1)
                {
                    pickuptimer.Stop();
                    // change timeout to 20 seconds
                    pickuptimer.Timeout = 20f;
                    Experior.Core.Environment.Log.Write("Setting timeout of pickuptimer " + pickuptimer.Timeout);
                    Experior.Core.Environment.Log.Write("RemainingTime of pickuptimer after setting timeout" + pickuptimer.TimeRemaining + " percentage remaining" + pickuptimer.PercentageRemaining);

                }
                else if (counter == 2)
                {
                    Experior.Core.Environment.Log.Write("Starting of pickuptimer ");
                    pickuptimer.Start();
                }
                else if (counter == 3)
                {
                    Experior.Core.Environment.Log.Write("Resetting of pickuptimer");
                    pickuptimer.Reset();
                }
                else if (counter == 4)
                {
                    Experior.Core.Environment.Log.Write("Starting of pickuptimer");
                    pickuptimer.Start();
                }
            }
            if (counter == 30)
            {
                Experior.Core.Environment.Log.Write("LogTimer will stop");
                sender.AutoReset = false;
                sender.Elapsed -= new Core.Timer.ElapsedEvent(logtimer_Elapsed);
                sender.Dispose();
            }
        }

        void pickuptimer_Elapsed(Core.Timer sender)
        {
            Experior.Core.Environment.Log.Write("PickupTimer has elapsed");
            pickuptimer.Dispose();
            pickuptimer = null;
        }

        public override string Category
        {
            get { return "ExampleTimer"; }
        }

        public override System.Drawing.Image Image
        {
            get { return Common.Icons.Get("Cube"); }
        }
    }
}
Was this article helpful?
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Previous Action