Exemple #1
0
            static void ProcessItem(TimerX timer)
            {
                // 删除过期的
                if (!timer.Callback.IsAlive)
                {
                    lock (timers)
                    {
                        timers.Remove(timer);
                        timer.Dispose();
                    }
                    return;
                }

                TimeSpan ts = timer.NextTime - DateTime.Now;
                Int32    d  = (Int32)ts.TotalMilliseconds;

                if (d > 0)
                {
                    // 缩小间隔,便于快速调用
                    if (d < period)
                    {
                        period = d;
                    }

                    return;
                }

                //WriteLog("TimerX.Process {0}", timer);

                try
                {
                    timer.Calling = true;

                    Action <Object> callback = timer.Callback;
                    // 线程池调用
                    if (timer.UseThreadPool)
                    {
                        ThreadPoolX.QueueUserWorkItem(delegate() { callback(timer.State); });
                    }
                    else
                    {
                        callback(timer.State);
                    }
                }
                catch (ThreadAbortException) { throw; }
                catch (ThreadInterruptedException) { throw; }
                catch { }
                finally
                {
                    timer.Timers++;
                    timer.Calling  = false;
                    timer.NextTime = DateTime.Now.AddMilliseconds(timer.Period);
                    if (timer.Period < period)
                    {
                        period = timer.Period;
                    }
                }
            }
Exemple #2
0
        /// <summary>实例化</summary>
        public ThreadItem(ThreadPoolX host)
        {
            Host = host ?? throw new ArgumentNullException(nameof(host));

            var th = Thread = new Thread(Work)
            {
                Name         = "P",
                IsBackground = true,
                //Priority = ThreadPriority.AboveNormal,
            };

            waitForTimer = new AutoResetEvent(false);
            ID           = th.ManagedThreadId;

            Active = true;
            th.Start();
        }
Exemple #3
0
 /// <summary>实例化</summary>
 public ThreadItem(ThreadPoolX host) => Host = host ?? throw new ArgumentNullException(nameof(host));