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. 从hello-world 开始 <contiki学习之四>

    按照contiki 官方给出的example下的例子之hello world来说,所有的工程里面都有一个唯一的Makefile.然后这个Makefile会去调用其他makefile文件.于是,一切就从 ...

  2. iOS 使用compare 进行对比

    compare 是 NSString 中的一个方法,这个方法是将字符串 按照 ACSII码表来进行对比. NSString *num1 = @"5.2.0"; NSString * ...

  3. stm32上的Lava虚拟机开发进度汇报(3)

    感觉遇到一个瓶颈了,这几天都没有什么进度. 前几天把函数和一些最基本的伪指令实现了一下,能跑一点仅使用了绘图函数的lav,但是函数调用的问题一直没解决. 后来发现是粗心漏写了个++,解决了函数调用的问 ...

  4. Codeforces Round #280 (Div. 2) E. Vanya and Field 数学

    E. Vanya and Field Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

  5. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  6. mbstowcs_s实现wchar_t转成char

    把char*转换为wchar_t* 用stdlib.h中的mbstowcs_s函数,可以通过下面的例子了解其用法: char*CStr = "string to convert"; ...

  7. [原]生产环境下的nginx.conf配置文件(多虚拟主机)

    [原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...

  8. Android实现图表绘制和展示

    本文演示在Android平台中绘制和展示图表示例,本示例是基于RChart 2实现的. 在一个系统中经常要用到图表统计数据,在WEB开发中图表绘制是一件简单的事情,因为有比较多的开源方案.但在Andr ...

  9. iOS开发——UI篇OC篇&UIView/UIWindow/UIScreen/CALayer

    UIView/UIWindow/UIScreen/CALayer 1.UIScreen可以获取设备屏幕的大小. 1 2 3 4 5 6 7 // 整个屏幕的大小 {{0, 0}, {320, 480} ...

  10. 基于 Jenkins 快速搭建持续集成环境--转

    源地址:http://www.ibm.com/developerworks/cn/java/j-lo-jenkins/ 持续集成是一种软件开发实践,对于提高软件开发效率并保障软件开发质量提供了理论基础 ...