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,通过它,可让您的项目及报表,轻松套用 ...
随机推荐
- 关于struts2 验证框架在联网的时候可以用,不联网不起作用的问题
这是一个让我很头痛的问题,我是在一个其他的项目框架的基础上来开发新的项目. 当使用struts验证框架时,突然发现这个验证不起作用了,我就纳闷了之前用这个开发的项目好好的怎么到我这就不能用了呢? xm ...
- 数据字符集mysql主从数据库,分库分表等笔记
文章结束给大家来个程序员笑话:[M] 1.mysql的目录:在rpm或者yum安装时:/var/lib/mysql 在编译安装时默许目录:/usr/local/mysql 2.用rpm包安装的MyS ...
- JAVA-开发环境搭建之JDK安装配置教程
在进行java开发前先要搭建java的开发环境 下载java的开发环境eclipse 安装&配置环境变量 1,JDK安装
- 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路
The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...
- or1200下raw-os(仿真环境篇)
貌似最近都在公司混日子过了,怎么办?哎哎哎~罪过啊罪过,不过也是的,加工资居然没我份,顶领导个肺的,叫我怎么继续活啊~哎哎哎~ 算了,不谈这些鸟事情了,说多了都是泪啊,这篇blog开始我们进入raw- ...
- 利用 Composer 一步一步构建自己的 PHP 框架(二)——构建路由
本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 上一篇中我们已经建立了一个空的 Composer 项目 ...
- SSIS 学习(5):容器【转】
容器是Integration Services 包中非常重要的一部分功能,它可以对控制流中的任务进行直观的划分与组织,使包的结构简明扼要.易于管理.易于维护. 在Integration Service ...
- Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器
第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...
- next_permutation()—遍历全排列
# next_permutation()--遍历全排列 template <class BidirectionalIterator> bool next_permutation (Bidi ...
- Hibernate悲观锁/乐观锁
如果需要保证数据访问的排它性,则需对目标数据加"锁",使其无法被其它程序修改 一,悲观锁 对数据被外界(包括本系统当前的其它事务和来自外部系统的事务处理)修改持保守态度,通过数据库 ...