winform timer 测试并行执行。
1,由于实际业务需要,需要设置定时执行某种活动。
如图。有四个按钮,和四个lable,还有四个timer,他们之间一一对应。但单击每一个按钮时,过两秒钟自动修改对应的label的值。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public partial class timerTest : Form
2 {
3 public timerTest()
4 {
5 InitializeComponent();
6 }
7
8
9 private void SetLabelOne()
10 {
11 this.lbTime1.Text = "Time1 is called";
12 }
13
14 private void SetLabelTwo()
15 {
16 this.lbTime2.Text = "Time2 is called";
17 }
18
19 private void SetLabelThree()
20 {
21 this.lbTime3.Text = "Time3 is called";
22 }
23
24 private void SetLabelFour()
25 {
26 this.lbTime4.Text = "Time4 is called";
27 }
28 private void btnTime1_Click(object sender, EventArgs e)
29 {
30 timer1.Enabled = true;
31 }
32
33 private void btnTime2_Click(object sender, EventArgs e)
34 {
35 timer2.Enabled = true;
36 }
37
38 private void btnTime3_Click(object sender, EventArgs e)
39 {
40 timer3.Enabled = true;
41 }
42
43 private void btnTime4_Click(object sender, EventArgs e)
44 {
45 timer4.Enabled = true;
46 }
47
48 private void timer1_Tick(object sender, EventArgs e)
49 {
50 SetLabelOne();
51 }
52
53 private void timer2_Tick(object sender, EventArgs e)
54 {
55 SetLabelTwo();
56 }
57
58 private void timer3_Tick(object sender, EventArgs e)
59 {
60 SetLabelThree();
61 }
62
63 private void timer4_Tick(object sender, EventArgs e)
64 {
65 SetLabelFour();
66 }
67 }
2 {
3 public timerTest()
4 {
5 InitializeComponent();
6 }
7
8
9 private void SetLabelOne()
10 {
11 this.lbTime1.Text = "Time1 is called";
12 }
13
14 private void SetLabelTwo()
15 {
16 this.lbTime2.Text = "Time2 is called";
17 }
18
19 private void SetLabelThree()
20 {
21 this.lbTime3.Text = "Time3 is called";
22 }
23
24 private void SetLabelFour()
25 {
26 this.lbTime4.Text = "Time4 is called";
27 }
28 private void btnTime1_Click(object sender, EventArgs e)
29 {
30 timer1.Enabled = true;
31 }
32
33 private void btnTime2_Click(object sender, EventArgs e)
34 {
35 timer2.Enabled = true;
36 }
37
38 private void btnTime3_Click(object sender, EventArgs e)
39 {
40 timer3.Enabled = true;
41 }
42
43 private void btnTime4_Click(object sender, EventArgs e)
44 {
45 timer4.Enabled = true;
46 }
47
48 private void timer1_Tick(object sender, EventArgs e)
49 {
50 SetLabelOne();
51 }
52
53 private void timer2_Tick(object sender, EventArgs e)
54 {
55 SetLabelTwo();
56 }
57
58 private void timer3_Tick(object sender, EventArgs e)
59 {
60 SetLabelThree();
61 }
62
63 private void timer4_Tick(object sender, EventArgs e)
64 {
65 SetLabelFour();
66 }
67 }
经过测试,这几个timer都可以独立运行,谁都不影响谁。