Three kinds of timing timers in C#: TIMER

There are three kinds of timers in .NET:
1. The Timer control under the System.Windows.Forms namespace, which directly inherits from Component. The Timer control will only automatically time after binding the Tick event and setting Enabled=True. Stop the time can be controlled by the Stop() method. After stopping by Stop(), if you want to re-time, you can use the Start() method to start the time. device. The Timer control and the Form where it is located belong to the same thread;
2. The Timer class under the System.Timers namespace. System.Timers.Timer class: Define a System.Timers.Timer object, then bind the Elapsed event, start the timing through the Start() method, and stop the timing through the Stop() method or Enable=false. The AutoReset property sets whether to repeat the timing (set to false to execute only once, set to true to execute multiple times). Elapsed event binding is equivalent to opening another thread, that is to say, controls in other threads cannot be accessed in events bound by Elapsed (delegates need to be defined, and the delegates are called through Invoke to access controls in other threads).
3, System.Threading.Timer class. When defining the class, initialize it through the constructor.
Among the three timers mentioned above, the first timer and the Form where it is located are in the same thread, so the execution efficiency is not high; and the methods executed by the second and third timers are all newly opened. One thread, so the execution efficiency is better than the first type of timer, so when choosing a timer, it is recommended to use the second and third types.

The following are examples of the use of three timers:

1. Timer control

Design interface:



Background code:

copy code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9
10 namespace TimerDemo
11 {
12     public partial class FrmMain : Form
13     {
14 //Define global variables
15         public int currentCount = 0;
16         public FrmMain()
17         {
18             InitializeComponent();
19         }
20
21         private void FrmMain_Load(object sender, EventArgs e)
22         {
23 //Set the Timer control to be available
24             this.timer.Enabled = true;
25 //Set the time interval (in milliseconds)
26             this.timer.Interval = 1000;
27         }
28
29         private void timer_Tick(object sender, EventArgs e)
30         {
31             currentCount += 1;
32             this.txt_Count.Text = currentCount.ToString().Trim();
33         }
34
35         private void btn_Start_Click(object sender, EventArgs e)
36         {
37 //Start timing
38             this.timer.Start();
39         }
40
41         private void btn_Stop_Click(object sender, EventArgs e)
42         {
43 //stop timing
44             this.timer.Stop();
45         }
46     }
47 }
copy code
2、System.Timers.Timer

Design interface:



Background code:

copy code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9
10 namespace TimersTimer
11 {
12     public partial class FrmMain : Form
13     {
14 //Define global variables
15         public int currentCount = 0;
16 //Define the Timer class
17 System.Timers.Timer timer;
18 //Define the delegate
19         public delegate void SetControlValue(string value);
20
21         public FrmMain()
22         {
23             InitializeComponent();
24         }
25
26         private void Form1_Load(object sender, EventArgs e)
27         {
28 InitTimer ();
29         }
30
31         /// <summary>
32 /// Initialize the Timer control
33         /// </summary>
34         private void InitTimer()
35         {
36 //Set the timing interval (in milliseconds)
37             int interval = 1000;
38             timer = new System.Timers.Timer(interval);
39 //Set to execute once (false) or always execute (true)
40             timer.AutoReset = true;
41 //Set whether to execute the System.Timers.Timer.Elapsed event
42             timer.Enabled = true;
43 //Bind the Elapsed event
44             timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
45         }
46
47         /// <summary>
48 /// The Timer class executes timed-to-point events
49         /// </summary>
50         /// <param name="sender"></param>
51         /// <param name="e"></param>
52         private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
53         {
54             try
55             {
56                 currentCount += 1;
57                 this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString());
58             }
59             catch (Exception ex)
60             {
61 MessageBox.Show("Failed to execute timed arrival event:" + ex.Message);
62             }
63         }
64
65         /// <summary>
66 /// Set the value of the text box
67         /// </summary>
68         /// <param name="strValue"></param>
69         private void SetTextBoxText(string strValue)
70         {
71             this.txt_Count.Text = this.currentCount.ToString().Trim();
72         }
73
74         private void btn_Start_Click(object sender, EventArgs e)
75         {
76 hours.Start ();
77         }
78
79         private void btn_Stop_Click(object sender, EventArgs e)
80         {
81 hours.Stop ();
82         }
83     }
84 }
copy code
3、System.Threading.Timer

Design interface:



Background code:

copy code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Threading;
10
11 namespace Threading.Timer
12 {
13     public partial class FrmMain : Form
14     {
15 //Define global variables
16         public int currentCount = 0;
17 //Define the Timer class
18         System.Threading.Timer threadTimer;
19 //Define the delegate
20         public delegate void SetControlValue(object value);
21
22         public FrmMain()
23         {
24             InitializeComponent();
25         }
26
27         private void FrmMain_Load(object sender, EventArgs e)
28         {
29 InitTimer ();
30         }
31
32         /// <summary>
33 /// Initialize the Timer class
34         /// </summary>
35         private void InitTimer()
36         {
37             threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);
38         }
39
40         /// <summary>
41 /// Events executed at timed to point
42         /// </summary>
43         /// <param name="value"></param>
44         private void TimerUp(object value)
45         {
46             currentCount += 1;
47             this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);
48         }
49
50         /// <summary>
51 /// Assign value to the text box
52         /// </summary>
53         /// <param name="value"></param>
54         private void SetTextBoxValue(object value)
55         {
56             this.txt_Count.Text = value.ToString();
57         }
58
59         /// <summary>
60 /// start
61         /// </summary>
62         /// <param name="sender"></param>
63         /// <param name="e"></param>
64         private void btn_Start_Click(object sender, EventArgs e)
65         {
66 //Start timing immediately, the time interval is 1000 milliseconds
67             threadTimer.Change(0, 1000);
68         }
69
70         /// <summary>
71 /// stop
72         /// </summary>
73         /// <param name="sender"></param>
74         /// <param name="e"></param>
75         private void btn_Stop_Click(object sender, EventArgs e)
76         {
77 //stop timing
78             threadTimer.Change(Timeout.Infinite, 1000);
79         }
80     }
81 }

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325004301&siteId=291194637