System.Windows.Forms.Timer反编译学习
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反编译学习的更多相关文章
- System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
- System.Windows.Forms.Timer
一.主要属性.方法和事件 Windows 窗体 Timer 是定期引发事件的组件.该组件是为 Windows 窗体环境设计的. 时间间隔的长度由 Interval 属性定义,其值以毫秒为单位.若启用了 ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...
- 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 ...
- System.Windows.Forms.Timer的简单用法
Timer就是用来计时操作,如:你想在多少秒之后执行某个动作 Timer showTextBoxTimer = new Timer(); //新建一个Timer对象 showTextBoxTimer. ...
- 命名空间“System.Windows.Forms”中不存在类型或命名空间名称“DataVisualization”。是否缺少程序集引用?
using System.Windows.Forms.DataVisualization.Charting; 编译时报警:命名空间"System.Windows.Forms"中不存 ...
- System.Windows.Forms
File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...
- .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll
这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...
随机推荐
- 批量下载QQ空间日志
从手机页面读取,有时候也会卡死,解决办法还是重新来……………… # -*-coding:utf-8-*- # 作者:fwindpeak # import urllib import urllib2 i ...
- 纯Javascript实现Windows 8 Metro风格实现
Metro风格设计主要特点 1.Windows 8 Metro风格设计,实现网站或系统功能的导航 2.纯Javascript实现 3.支持所有IE.360.Chrome等常用浏览器 4.支持圆角.阴影 ...
- utf-8 和gbk编码的差别
UTF- 8: 是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码. GBK 是国家标准GB2312基础上扩容后兼容GB2312的标准. GBK的文 ...
- delphi 16 网页缩放
网页放大 网页缩小 WebBrowser1.OleObject.Document.Body.Style.Zoom := 0.50; 缩放网页 Ctrl+中键↑ 放大 Ctrl+中键↓ ...
- 使用自定义的BaseAdapter实现LIstView的展示
http://stephen830.iteye.com/blog/1141394 使用自定义的BaseAdapter实现LIstView的展示 实现以下功能点: 1.通过自定义的BaseAdapter ...
- Android 进阶学习:事件分发机制全然解析,带你从源代码的角度彻底理解(上)
http://blog.csdn.net/guolin_blog/article/details/9097463 事实上我一直准备写一篇关于Android事件分发机制的文章,从我的第一篇博客開始,就零 ...
- iOS小结
一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始等待用户的操作,自动释放 ...
- Android中Activity启动模式详解
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...
- CSAPP缓冲区溢出攻击实验(上)
CSAPP缓冲区溢出攻击实验(上) 下载实验工具.最新的讲义在这. 网上能找到的实验材料有些旧了,有的地方跟最新的handout对不上.只是没有关系,大体上仅仅是程序名(sendstring)或者參数 ...
- zh-cn,zh-tw,en-us,en-gb等网页语言代码一览表
zh-cn 简体中文zh-tw 繁体中文da-dk 丹麦语nl-nl 荷兰语en-us 英语fi-fi 芬兰语fr-fr 法语de-de 德语it-it 意大利语j ...