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,通过它,可让您的项目及报表,轻松套用 ...
随机推荐
- 检查dns服务器是否可用
#%windir%\system32\WindowsPowerShell\v1.0\powershell.exe D:\PSScript\ERP_Production_Script\ERPRF_Upd ...
- Swift学习笔记五
基础运算符 Swift的大部分运算符和C及OC相同,也分一元二元多元的,这里只记录一些Swift特有的性质或写法. 赋值运算符( = ) 在等号右边是一个有多个值的元组时,它的成员值可以分解并同时分别 ...
- GLSL实现Fresnel And Chromatic aberration 【转】
http://blog.csdn.net/a3070173/archive/2008/11/13/3290091.aspx 使用Shader实现菲涅尔和颜色色散效果很简单,在Cg教程和OpenGL S ...
- delphi 自动滚动到最底端scroll
自动滚动到最底端scrollUses MSHTML;{$R *.dfm}var ScrollPos: integer=0;procedure TForm1.Button1Click(Sender: ...
- 【转】Linux下(C/C++)使用system()函数一定要谨慎
转自:http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用 ...
- android圆角View实现及不同版本这间的兼容(android3.0过后的版本)
http://blog.csdn.net/lovecluo/article/details/8710174 在做我们自己的APP的时候,为了让APP看起来更加的好看,我们就需要将我们的自己的View做 ...
- Android之使用AchartEngineActivity引擎绘制柱状图、曲线图
1.简介 AChartEngine(简称ACE)是Google的一个开源图表库(for Android).它功能强大,支持散点图.折线 .关于里面类的具体使用,请下载响应的文档说明(主页上有). 2. ...
- fedora 20 注销
当系统只有一个用户和只有一个桌面环境时,Fedora 20将不会显示Log Out菜单. 如果你确实需要logout,可以通过执行gnome-session-quit命令来logout. 如果你确实需 ...
- 通过tracing技术来教授操作系统
https://www.cl.cam.ac.uk/teaching/1516/L41/materials.html https://github.com/myaut/dtrace-stap-book
- MHA手动切换 原创1(主故障)
MHA提供了3种方式用于实现故障转移,分别自动故障转移,需要启用MHA监控: 在无监控的情况下的手动故障转移以及基于在线手动切换. 三种方式可以应对MySQL主从故障的任意场景.本文主要描述在无监控的 ...