有一次面试,面试官说卡顿怎么处理,我说多线程。多线程比timer好读。
好了,现在看看timer和线程的关系。
timer有3种
1.winform 下的timer。就是拖控件到UI上的那个timer.
源文件在这个路径下C:\Windows\Microsoft.NET\Framework64\v4.0.30319
namespace System.Windows.Forms
{
// 摘要: 实现按用户定义的时间间隔引发事件的计时器。 此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中 使用。
[DefaultEvent("Tick")]
[DefaultProperty("Interval")][SRDescriptionAttribute("DescriptionTimer")][ToolboxItemFilter("System.Windows.Forms")]
public class Timer : Component
}
启动timer代码如下:
[SRCategory("CatBehavior")]
[DefaultValue(false)]
[SRDescription("TimerEnabledDescr")]
public virtual bool Enabled
{
get
{
if (this.timerWindow == null)
{
return this.enabled;
}
return this.timerWindow.IsTimerRunning;
}
set
{
lock (this.syncObj)
{
if (this.enabled != value)
{
this.enabled = value;
if (!base.DesignMode)
{
if (value)
{
if (this.timerWindow == null)
{
this.timerWindow = new TimerNativeWindow(this);
}
this.timerRoot = GCHandle.Alloc(this);
this.timerWindow.StartTimer(this.interval);
}
else
{
if (this.timerWindow != null)
{
this.timerWindow.StopTimer();
}
if (this.timerRoot.IsAllocated)
{
this.timerRoot.Free();
}
}
}
}
}
}
}
最终调用了this.timerWindow.StartTimer(this.interval); 源码如下。可见,最终调用的是系统的timer?系统是有定时器的。Ucos上,就有32个定时器,当然也可以开线程。他们是不同的概念。windows 也差不多吧。这些定时器应该与CPU有关。
public void StartTimer(int interval)
{
if (this._timerID == 0 && !this._stoppingTimer && this.EnsureHandle())
{
this._timerID = (int)SafeNativeMethods.SetTimer(new HandleRef(this, base.Handle), TimerNativeWindow.TimerID++, interval, IntPtr.Zero);
}
}
2. public sealed class Timer : MarshalByRefObject, IDisposable
System.Threading.Timer
public Timer(TimerCallback callback)
{
int dueTime = -1;
int period = -1;
StackCrawlMark stackCrawlMark = StackCrawlMark.LookForMyCaller;
this.TimerSetup(callback, this, (uint)dueTime, (uint)period, ref stackCrawlMark);
}
[SecurityCritical]
private void TimerSetup(TimerCallback callback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
{
if (callback == null)
{
throw new ArgumentNullException("TimerCallback");
}
this.m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period, ref stackMark));
}
[SecurityCritical]
internal static void Pause()
{
TimerQueue.Instance.Pause();
}
[SecurityCritical]
internal static void Resume()
{
TimerQueue.Instance.Resume();
}
这里是TimerQueue 队列的操作。既然在Threading 命名空间下,可能与线程有关。他在的dll 是 mscorlib.
3. System.Timers.Timer, 在system.dll中。 只是对 System.Threading.Timer的封装。
[TimersDescription("TimerEnabled")]
[DefaultValue(false)]
public bool Enabled
{
get
{
return this.enabled;
}
set
{
if (base.DesignMode)
{
this.delayedEnable = value;
this.enabled = value;
}
else if (this.initializing)
{
this.delayedEnable = value;
}
else if (this.enabled != value)
{
if (!value)
{
if (this.timer != null)
{
this.cookie = null;
this.timer.Dispose();
this.timer = null;
}
this.enabled = value;
}
else
{
this.enabled = value;
if (this.timer == null)
{
if (this.disposed)
{
throw new ObjectDisposedException(base.GetType().Name);
}
int num = (int)Math.Ceiling(this.interval);
this.cookie = new object();
this.timer = new System.Threading.Timer(this.callback, this.cookie, num, this.autoReset ? num : (-1));
}
else
{
this.UpdateTimer();
}
}
}
}
}
4.使用:
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
if (timer != null)
{
timer.Stop();
timer.Close();
timer = null;
}
int Interval = 3600000;//6 hours
timer = new System.Timers.Timer(Interval);//十分钟
timer.Elapsed += SendSMS.Send_ticker;
timer.Interval = Interval;
timer.Enabled = true;
timer.Start();
}