⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 globaltimer.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace Imps.Utils
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;

    public static class GlobalTimer
    {
        private static List<TickHandler> _handlers = new List<TickHandler>();
        private static Dictionary<IComponent, List<EventHandler>> _hosts = new Dictionary<IComponent, List<EventHandler>>();

        private static int FindHandler(EventHandler handler)
        {
            lock (Handlers)
            {
                for (int i = 0; i < Handlers.get_Count(); i++)
                {
                    if (Handlers.get_Item(i).Handler == handler)
                    {
                        return i;
                    }
                }
                return -1;
            }
        }

        private static void host_Disposed(object sender, EventArgs e)
        {
            try
            {
                Unregister((IComponent) sender);
            }
            catch
            {
            }
        }

        public static bool IsHandlerRegistered(EventHandler handler)
        {
            return (FindHandler(handler) >= 0);
        }

        public static void OnTick(object sender, EventArgs e)
        {
            TickHandler[] handlerArray;
            int tickCount = Environment.TickCount;
            bool flag = false;
            lock (Handlers)
            {
                handlerArray = Handlers.ToArray();
            }
            foreach (TickHandler handler in handlerArray)
            {
                if (flag)
                {
                    handler.Counter++;
                }
                else
                {
                    try
                    {
                        handler.OnTick(sender, e);
                    }
                    catch (Exception exception)
                    {
                        Trace.WriteLine(exception.ToString());
                    }
                    if (((Environment.TickCount - tickCount) > 0x3e8) && (handler.Priority != TimerHandlerPriority.Highest))
                    {
                        flag = true;
                    }
                }
            }
        }

        public static void Register(EventHandler handler)
        {
            Register(handler, null, 1);
        }

        public static void Register(EventHandler handler, IComponent host)
        {
            Register(handler, host, 1);
        }

        public static void Register(EventHandler handler, int interval)
        {
            Register(handler, null, interval);
        }

        public static void Register(EventHandler handler, IComponent host, int interval)
        {
            Register(handler, host, interval, TimerHandlerPriority.Normal);
        }

        public static void Register(EventHandler handler, int interval, TimerHandlerPriority priority)
        {
            Register(handler, null, interval, priority);
        }

        public static void Register(EventHandler handler, IComponent host, int interval, TimerHandlerPriority priority)
        {
            if (interval < 1)
            {
                throw new ArgumentOutOfRangeException("interval");
            }
            lock (Handlers)
            {
                bool flag = true;
                int num = FindHandler(handler);
                if (num >= 0)
                {
                    Handlers.get_Item(num).Interval = interval;
                    flag = Handlers.get_Item(num).Priority != priority;
                    Handlers.get_Item(num).Priority = priority;
                }
                else
                {
                    Handlers.Add(new TickHandler(handler, interval, priority));
                }
                if (flag)
                {
                    Handlers.Sort();
                }
                if (host != null)
                {
                    List<EventHandler> list;
                    if (Hosts.ContainsKey(host))
                    {
                        list = Hosts.get_Item(host);
                    }
                    else
                    {
                        list = new List<EventHandler>();
                        Hosts.Add(host, list);
                        host.Disposed += new EventHandler(GlobalTimer.host_Disposed);
                    }
                    list.Add(handler);
                }
            }
        }

        public static void Unregister(IComponent host)
        {
            lock (Handlers)
            {
                if (Hosts.ContainsKey(host))
                {
                    List<EventHandler>.Enumerator enumerator = Hosts.get_Item(host).GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            Unregister(enumerator.get_Current());
                        }
                    }
                    finally
                    {
                        enumerator.Dispose();
                    }
                    Hosts.Remove(host);
                }
            }
        }

        public static void Unregister(EventHandler handler)
        {
            lock (Handlers)
            {
                int num = FindHandler(handler);
                if (num >= 0)
                {
                    Handlers.RemoveAt(num);
                }
            }
        }

        private static List<TickHandler> Handlers
        {
            get
            {
                return _handlers;
            }
        }

        private static Dictionary<IComponent, List<EventHandler>> Hosts
        {
            get
            {
                return _hosts;
            }
        }

        private class TickHandler : IComparable<GlobalTimer.TickHandler>
        {
            private int _counter;
            private EventHandler _handler;
            private int _interval;
            private TimerHandlerPriority _priority;

            public TickHandler(EventHandler handler, int interval, TimerHandlerPriority priority)
            {
                this._handler = handler;
                this._priority = priority;
                if (priority == TimerHandlerPriority.Highest)
                {
                    this._interval = interval * 100;
                    this._counter = Environment.TickCount;
                }
                else
                {
                    this._interval = interval;
                    this._counter = 1;
                }
            }

            public int CompareTo(GlobalTimer.TickHandler other)
            {
                return (int) (other.Priority - this.Priority);
            }

            internal void OnTick(object sender, EventArgs e)
            {
                if (this._handler != null)
                {
                    if (this.Priority == TimerHandlerPriority.Highest)
                    {
                        int tickCount = Environment.TickCount;
                        if ((tickCount - this._counter) >= this._interval)
                        {
                            this._handler(sender, e);
                            this._counter = tickCount;
                        }
                    }
                    else if (this._counter >= this._interval)
                    {
                        this._counter = 1;
                        this._handler(sender, e);
                    }
                    else
                    {
                        this._counter++;
                    }
                }
            }

            internal int Counter
            {
                get
                {
                    return this._counter;
                }
                set
                {
                    this._counter = value;
                }
            }

            public EventHandler Handler
            {
                get
                {
                    return this._handler;
                }
            }

            public int Interval
            {
                get
                {
                    if (this.Priority != TimerHandlerPriority.Highest)
                    {
                        return this._interval;
                    }
                    return (this._interval / 100);
                }
                set
                {
                    if (this.Priority == TimerHandlerPriority.Highest)
                    {
                        this._interval = value * 100;
                        this._counter = Environment.TickCount;
                    }
                    else
                    {
                        this._interval = value;
                        this._counter = 1;
                    }
                }
            }

            public TimerHandlerPriority Priority
            {
                get
                {
                    return this._priority;
                }
                set
                {
                    if (this._priority != value)
                    {
                        if (this._priority == TimerHandlerPriority.Highest)
                        {
                            this._interval /= 100;
                            this._counter = 1;
                        }
                        else
                        {
                            this._interval *= 100;
                            this._counter = Environment.TickCount;
                        }
                        this._priority = value;
                    }
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -