using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Threading;
namespace System.Windows.Forms
{
[DefaultEvent("Tick"), DefaultProperty("Interval"), ToolboxItemFilter("System.Windows.Forms"), SRDescription("DescriptionTimer")]
public class Timer : Component
{
private class TimerNativeWindow : NativeWindow
{
private Timer _owner;
private int _timerID;
private static int TimerID = ;
private bool _stoppingTimer;
public bool IsTimerRunning
{
get
{
return this._timerID != && base.Handle != IntPtr.Zero;
}
}
~TimerNativeWindow()
{
this.StopTimer();
}
public void RestartTimer(int newInterval)
{
this.StopTimer(false, IntPtr.Zero);
this.StartTimer(newInterval);
}
public void StartTimer(int interval)
{
if (this._timerID == && !this._stoppingTimer && this.EnsureHandle())
{
this._timerID = (int)SafeNativeMethods.SetTimer(new HandleRef(this, base.Handle), Timer.TimerNativeWindow.TimerID++, interval, IntPtr.Zero);
}
}
public void StopTimer()
{
this.StopTimer(true, IntPtr.Zero);
}
public void StopTimer(bool destroyHwnd, IntPtr hWnd)
{
if (hWnd == IntPtr.Zero)
{
hWnd = base.Handle;
}
if (this.GetInvokeRequired(hWnd))
{
UnsafeNativeMethods.PostMessage(new HandleRef(this, hWnd), , , );
return;
}
bool flag = false;
try
{
Monitor.Enter(this, ref flag);
if (!this._stoppingTimer && !(hWnd == IntPtr.Zero) && UnsafeNativeMethods.IsWindow(new HandleRef(this, hWnd)))
{
if (this._timerID != )
{
try
{
this._stoppingTimer = true;
SafeNativeMethods.KillTimer(new HandleRef(this, hWnd), this._timerID);
}
finally
{
this._timerID = ;
this._stoppingTimer = false;
}
}
if (destroyHwnd)
{
base.DestroyHandle();
}
}
}
finally
{
if (flag)
{
Monitor.Exit(this);
}
}
}
public override void DestroyHandle()
{
this.StopTimer(false, IntPtr.Zero);
base.DestroyHandle();
}
protected override void OnThreadException(Exception e)
{
Application.OnThreadException(e);
}
public override void ReleaseHandle()
{
this.StopTimer(false, IntPtr.Zero);
base.ReleaseHandle();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == )
{
if ((int)((long)m.WParam) == this._timerID)
{
this._owner.OnTick(EventArgs.Empty);
return;
}
}
else
{
if (m.Msg == )
{
this.StopTimer(true, m.HWnd);
return;
}
}
base.WndProc(ref m);
}
internal TimerNativeWindow(Timer owner)
{
this._owner = owner;
}
private bool EnsureHandle()
{
if (base.Handle == IntPtr.Zero)
{
CreateParams createParams = new CreateParams();
createParams.Style = ;
createParams.ExStyle = ;
createParams.ClassStyle = ;
createParams.Caption = base.GetType().Name;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
createParams.Parent = (IntPtr)NativeMethods.HWND_MESSAGE;
}
this.CreateHandle(createParams);
}
return base.Handle != IntPtr.Zero;
}
private bool GetInvokeRequired(IntPtr hWnd)
{
if (hWnd != IntPtr.Zero)
{
int num;
int windowThreadProcessId = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, hWnd), out num);
int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
return windowThreadProcessId != currentThreadId;
}
return false;
}
}
private int interval;
private bool enabled;
private EventHandler onTimer;
private GCHandle timerRoot;
private Timer.TimerNativeWindow timerWindow;
private object userData;
private object syncObj = new object();
[SRCategory("CatBehavior"), SRDescription("TimerTimerDescr")]
public event EventHandler Tick
{
add
{
this.onTimer = (EventHandler)Delegate.Combine(this.onTimer, value);
}
remove
{
this.onTimer = (EventHandler)Delegate.Remove(this.onTimer, value);
}
}
[Bindable(true), DefaultValue(null), Localizable(false), TypeConverter(typeof(StringConverter)), SRCategory("CatData"), SRDescription("ControlTagDescr")]
public object Tag
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.userData;
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
set
{
this.userData = value;
}
}
[DefaultValue(false), SRCategory("CatBehavior"), 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 Timer.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();
}
}
}
}
}
}
}
[DefaultValue(), SRCategory("CatBehavior"), SRDescription("TimerIntervalDescr")]
public int Interval
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.interval;
}
set
{
lock (this.syncObj)
{
if (value < )
{
throw new ArgumentOutOfRangeException("Interval", SR.GetString("TimerInvalidInterval", new object[]
{
value,
.ToString(CultureInfo.CurrentCulture)
}));
}
if (this.interval != value)
{
this.interval = value;
if (this.Enabled && !base.DesignMode && this.timerWindow != null)
{
this.timerWindow.RestartTimer(value);
}
}
}
}
}
public Timer()
{
this.interval = ;
}
public Timer(IContainer container) : this()
{
if (container == null)
{
throw new ArgumentNullException("container");
}
container.Add(this);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.timerWindow != null)
{
this.timerWindow.StopTimer();
}
this.Enabled = false;
}
this.timerWindow = null;
base.Dispose(disposing);
}
protected virtual void OnTick(EventArgs e)
{
if (this.onTimer != null)
{
this.onTimer(this, e);
}
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Start()
{
this.Enabled = true;
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Stop()
{
this.Enabled = false;
}
public override string ToString()
{
string str = base.ToString();
return str + ", Interval: " + this.Interval.ToString(CultureInfo.CurrentCulture);
}
}
}

以上代码是通过ILSpy反编译出来的,学习的心得如下:

1、Start和Stop方法,都是通过设置Enabled属性启用或停用定时器

2、Interval用来设置定时器的运行时间间隔

3、设置Interval注意以下代码:

 public int Interval
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.interval;
}
set
{
lock (this.syncObj)
{
if (value < )
{
throw new ArgumentOutOfRangeException("Interval", SR.GetString("TimerInvalidInterval", new object[]
{
value,
.ToString(CultureInfo.CurrentCulture)
}));
}
if (this.interval != value)
{
this.interval = value;
if (this.Enabled && !base.DesignMode && this.timerWindow != null)
{
this.timerWindow.RestartTimer(value);
}
}
}
}
}

4、默认Timter的时间间隔为100毫秒。

 public Timer()
{
this.interval = ;
}

5、重写了ToString方法

 public override string ToString()
{
string str = base.ToString();
return str + ", Interval: " + this.Interval.ToString(CultureInfo.CurrentCulture);
}

System.Windows.Forms.Timer反编译学习的更多相关文章

  1. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  2. System.Windows.Forms.Timer

    一.主要属性.方法和事件 Windows 窗体 Timer 是定期引发事件的组件.该组件是为 Windows 窗体环境设计的. 时间间隔的长度由 Interval 属性定义,其值以毫秒为单位.若启用了 ...

  3. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  4. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  5. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

    System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...

  6. System.Windows.Forms.Timer的简单用法

    Timer就是用来计时操作,如:你想在多少秒之后执行某个动作 Timer showTextBoxTimer = new Timer(); //新建一个Timer对象 showTextBoxTimer. ...

  7. 命名空间“System.Windows.Forms”中不存在类型或命名空间名称“DataVisualization”。是否缺少程序集引用?

    using System.Windows.Forms.DataVisualization.Charting; 编译时报警:命名空间"System.Windows.Forms"中不存 ...

  8. System.Windows.Forms

    File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...

  9. .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll

    这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...

随机推荐

  1. 杭电 2034 人见人爱A-B

    http://acm.hdu.edu.cn/showproblem.php?pid=2034 人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. 1039. Course List for Student (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1039 题目: 1039. Course List for Student (25) 时间限制 2 ...

  3. Java 中队列的使用

    刚才看见群里的一个朋友在问队列的使用,确实在现实的写代码中非常少使用队列的,反正我是从来没使用过.仅仅是学数据结构的时候学过. 以下是我写的一个小样例,希望有不足之处请提出改正.O(∩_∩)O~ 看代 ...

  4. stl 容器

    10.1.2.2容器的分类 序列式容器(Sequence containers) 每个元素都有固定位置--取决于插入时机和地点,和元素值无关. vector.deque.list  关联式容器(Ass ...

  5. quickstack is a tool to take call stack

    https://github.com/yoshinorim/quickstack quickstack is a tool to take call stack traces with minimal ...

  6. Linux内核--网络栈实现分析(一)--网络栈初始化--转

    转载地址 http://blog.csdn.net/yming0221/article/details/7488828 作者:闫明 本文分析基于内核Linux Kernel 1.2.13 以后的系列博 ...

  7. c语言指针难点

    先来一个例子 例: #include "stdio.h" int main() { ] = {,,,,}; printf("a是一个地址%d\n",a); pr ...

  8. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  9. Honda HDS IMMO PCM Code calculator Free Download

    HDS IMMO PCM Code calculator software for Honda vehicle models is free download available in Eobd2.f ...

  10. ASP.NET MVC and jqGrid 学习笔记 4-排序

    排序(Sorting)分为两种:客户端排序和服务端排序 客户端排序的意思是把数据从数据库里一次性全部提取出来,然后在客户端进行排序,以后每次点击标题进行排序时,就不会给服务端传递请求了.这个“一次性” ...