winform timer 测试并行执行。

1,由于实际业务需要,需要设置定时执行某种活动。

如图。有四个按钮,和四个lable,还有四个timer,他们之间一一对应。但单击每一个按钮时,过两秒钟自动修改对应的label的值。


 

代码如下:

 

ExpandedBlockStart.gif View Code
 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      }

 

 经过测试,这几个timer都可以独立运行,谁都不影响谁。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值