Solved How to create timer in C#

xandeturf

Moderator
Moderator
Nov 4, 2015
132
25
28
35
i used the example Howto C# timer and not work help me !

example:
CreateParallelTimer(2000, d).Start();

not work in c# module
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
i used the example Howto C# timer and not work help me !

example:
CreateParallelTimer(2000, d).Start();

not work in c# module
What is your timer called? and what is the callback name you used?

EG:
Plugin.CreateTimer("PingCheck",5000).Start();

void PingCheckCallback(ATimedEvent TimeEvent)
{
//Something here
}
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
i used the example Howto C# timer and not work help me !

example:
CreateParallelTimer(2000, d).Start();

not work in c# module
For NON C# plugins: http://fougerite.com/wiki/timer-setups/

C#:

C#:
    public class PluginTimedEvent
    {

        private Dictionary<string, object> _args;
        private readonly System.Timers.Timer _timer;
        private long lastTick;
        private int _elapsedCount;

        internal delegate void TimedEventFireDelegate(PluginTimedEvent evt);

        internal event TimedEventFireDelegate OnFire;

        internal PluginTimedEvent(double interval)
        {
            this._timer = new System.Timers.Timer();
            this._timer.Interval = interval;
            this._timer.Elapsed += new ElapsedEventHandler(this._timer_Elapsed);
            this._elapsedCount = 0;
        }

        internal PluginTimedEvent(double interval, Dictionary<string, object> args)
            : this(interval)
        {
            this.Args = args;
        }

        private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (this.OnFire != null)
            {
                this.OnFire(this);
            }

            this._elapsedCount += 1;
            this.lastTick = DateTime.UtcNow.Ticks;
        }

        internal void Start()
        {
            this._timer.Start();
            this.lastTick = DateTime.UtcNow.Ticks;
        }

        internal void Stop()
        {
            this._timer.Stop();
        }

        internal void Kill()
        {
            this._timer.Stop();
            this._timer.Dispose();
        }

        internal Dictionary<string, object> Args
        {
            get { return this._args; }
            set { this._args = value; }
        }

        internal double Interval
        {
            get { return this._timer.Interval; }
            set { this._timer.Interval = value; }
        }

        internal double TimeLeft
        {
            get { return (this.Interval - ((DateTime.UtcNow.Ticks - this.lastTick) / 0x2710L)); }
        }

        internal int ElapsedCount
        {
            get { return this._elapsedCount; }
        }
    }
Usage:

C#:
internal PluginTimedEvent CreateParallelTimer(int timeoutDelay, Dictionary<string, object> args)
{
    PluginTimedEvent timedEvent = new PluginTimedEvent (timeoutDelay);
    timedEvent.Args = args;
    timedEvent.OnFire += Callback;
    return timedEvent;
}

internal void Callback(PluginTimedEvent e)
{
    e.Kill();
    var data = e.Args;
    Fougerite.Player player = (Fougerite.Player) data["Player"];
    //todo do something here
}

public void OnPlayerSpawned(Fougerite.Player player, SpawnEvent se)
{
    var dict = new Dictionary<string, object>();
    dict["Player"] = player;
    CreateParallelTimer(5000, dict).Start();
}
 
  • Like
Reactions: Jakkee