源文件: http://pan.baidu.com/share/link?shareid=3199918884&uk=3912660076
System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
资料参考来源:
http://www.cnblogs.com/shang20017/archive/2009/03/16/1413798.html
http://hi.baidu.com/dreamyguy/item/9102485fb038753494eb05ef
http://www.shabdar.org/c-sharp/102-cross-thread-operation-not-valid.html
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/zh-cn/library/system.datetime.aspx
http://msdn.microsoft.com/zh-cn/library/system.timers.timer.aspx
http://msdn.microsoft.com/en-us/library/ms171728.aspx
参考代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 计时器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region Way1 :http://msdn.microsoft.com/zh-cn/library/system.windows.forms.timer.aspx System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();//1. int i = 0; private void btnStart_Click(object sender, EventArgs e) { myTimer.Interval = 1;//每一毫秒触发一次事件 // myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(this.myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { lblInterval.Text = "本次程序用时结果: " + (++i).ToString() + " 单位"; } private void btnEnd_Click(object sender, EventArgs e) { myTimer.Stop(); //myTimer.Enabled = false; } #endregion public string Timer1(bool set) { string Value = string.Empty; if (set) { } return Value; } //................................... #region Way2 : http://msdn.microsoft.com/zh-cn/library/system.timers.timer.aspx delegate void SetTextCallback(string text); int i2 = 0; DateTime myDatetime1 = new DateTime(); DateTime myDatetime2 = new DateTime(); System.Timers.Timer myTimer2 = new System.Timers.Timer(1); private void btnStart2_Click(object sender, EventArgs e) { myTimer2.Enabled = true;// myTimer2.Start(); myTimer2.AutoReset = true; myTimer2.Elapsed += new System.Timers.ElapsedEventHandler(myTimer2_Elapsed); myDatetime1 = DateTime.Now; } void myTimer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Settext((++i).ToString()); } private void btnEnd2_Click(object sender, EventArgs e) { //myTimer2.Stop(); myTimer2.Enabled = false; myDatetime2 = DateTime.Now; TimeSpan Subdt=myDatetime2.Subtract(myDatetime1); MessageBox.Show(Subdt.TotalMilliseconds.ToString()); } private void Settext(string text) { if (this.lblInterval.InvokeRequired) { SetTextCallback cb = new SetTextCallback(Settext); this.Invoke(cb, new object[] { text }); } else { this.lblInterval.Text = "本次程序用时结果: " + text + " 单位"; } } #endregion #region Way3 : http://msdn.microsoft.com/zh-cn/library/system.threading.timer.aspx #endregion } }
namespace 计时器 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btnStart = new System.Windows.Forms.Button(); this.btnEnd = new System.Windows.Forms.Button(); this.lblInterval = new System.Windows.Forms.Label(); this.grpbxOne = new System.Windows.Forms.GroupBox(); this.grpbxWay2 = new System.Windows.Forms.GroupBox(); this.btnEnd2 = new System.Windows.Forms.Button(); this.btnStart2 = new System.Windows.Forms.Button(); this.grpbxOne.SuspendLayout(); this.grpbxWay2.SuspendLayout(); this.SuspendLayout(); // // btnStart // this.btnStart.Location = new System.Drawing.Point(0, 127); this.btnStart.Name = "btnStart"; this.btnStart.Size = new System.Drawing.Size(75, 23); this.btnStart.TabIndex = 0; this.btnStart.Text = "Start"; this.btnStart.UseVisualStyleBackColor = true; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // btnEnd // this.btnEnd.Location = new System.Drawing.Point(0, 156); this.btnEnd.Name = "btnEnd"; this.btnEnd.Size = new System.Drawing.Size(75, 23); this.btnEnd.TabIndex = 1; this.btnEnd.Text = "End"; this.btnEnd.UseVisualStyleBackColor = true; this.btnEnd.Click += new System.EventHandler(this.btnEnd_Click); // // lblInterval // this.lblInterval.AutoSize = true; this.lblInterval.Location = new System.Drawing.Point(12, 9); this.lblInterval.Name = "lblInterval"; this.lblInterval.Size = new System.Drawing.Size(0, 13); this.lblInterval.TabIndex = 2; // // grpbxOne // this.grpbxOne.Controls.Add(this.btnStart); this.grpbxOne.Controls.Add(this.btnEnd); this.grpbxOne.Location = new System.Drawing.Point(12, 35); this.grpbxOne.Name = "grpbxOne"; this.grpbxOne.Size = new System.Drawing.Size(75, 185); this.grpbxOne.TabIndex = 3; this.grpbxOne.TabStop = false; this.grpbxOne.Text = "Way1"; // // grpbxWay2 // this.grpbxWay2.Controls.Add(this.btnEnd2); this.grpbxWay2.Controls.Add(this.btnStart2); this.grpbxWay2.Location = new System.Drawing.Point(94, 35); this.grpbxWay2.Name = "grpbxWay2"; this.grpbxWay2.Size = new System.Drawing.Size(76, 185); this.grpbxWay2.TabIndex = 4; this.grpbxWay2.TabStop = false; this.grpbxWay2.Text = "Way2"; // // btnEnd2 // this.btnEnd2.Location = new System.Drawing.Point(0, 156); this.btnEnd2.Name = "btnEnd2"; this.btnEnd2.Size = new System.Drawing.Size(75, 23); this.btnEnd2.TabIndex = 1; this.btnEnd2.Text = "End"; this.btnEnd2.UseVisualStyleBackColor = true; this.btnEnd2.Click += new System.EventHandler(this.btnEnd2_Click); // // btnStart2 // this.btnStart2.Location = new System.Drawing.Point(0, 127); this.btnStart2.Name = "btnStart2"; this.btnStart2.Size = new System.Drawing.Size(75, 23); this.btnStart2.TabIndex = 0; this.btnStart2.Text = "Start"; this.btnStart2.UseVisualStyleBackColor = true; this.btnStart2.Click += new System.EventHandler(this.btnStart2_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(261, 231); this.Controls.Add(this.grpbxWay2); this.Controls.Add(this.grpbxOne); this.Controls.Add(this.lblInterval); this.Name = "Form1"; this.Text = "Form1"; this.grpbxOne.ResumeLayout(false); this.grpbxWay2.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Button btnEnd; private System.Windows.Forms.Label lblInterval; private System.Windows.Forms.GroupBox grpbxOne; private System.Windows.Forms.GroupBox grpbxWay2; private System.Windows.Forms.Button btnEnd2; private System.Windows.Forms.Button btnStart2; } }