Ejemplo n.º 1
0
        public string SetTimeout(int delay, ScriptTimerHandler callback, bool loop = false)
        {
            if (w != null)
            {
                ScriptTimer timer = w.CreateTimer(this, delay, loop);

                if (timer == null)
                {
                    return(null);
                }

                ((ILease)timer.GetLifetimeService()).Register(w);

                if (!TimerHandlers.ContainsKey(timer))
                {
                    TimerHandlers.Add(timer, callback);
                }
                //TimerHandlers[timer] = callback;

                timer.Start();

                return(timer.ID);
            }

            return(null);
        }
Ejemplo n.º 2
0
        internal ScriptTimer(ServerScript caller, int interval, ScriptTimerHandler callback, bool loop = false)
        {
            if (Wrapper.SEScriptTimer == null || Wrapper.SEScriptTimerFunction == null || Wrapper.SEScriptTimerTickFrom == null || Wrapper.SEScriptTimerList == null)
            {
                return;
            }

            this.caller = caller;

            ID = Guid.NewGuid().ToString();

            lock (Wrapper.ScriptTimers)
            {
                while (Wrapper.ScriptTimers.Any(st => st.ID == ID))
                {
                    ID = Guid.NewGuid().ToString();                                                 // Not taking any chances.
                }
                Wrapper.ScriptTimers.Add(this);
            }

            SEScriptTimer = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(Wrapper.SEScriptTimer.Assembly.FullName, Wrapper.SEScriptTimer.FullName);

            Interval = interval;

            this.loop = loop;

            Handler = callback;

            Function = new Action(() =>
            {
                if (Handler != null)
                {
                    Handler(this);
                }

                if (Loop)
                {
                    Start();
                }
                else
                {
                    Cancel();
                }
            });
        }