winform timer 測試並存執行。

來源:互聯網
上載者:User

1,由於實際業務需要,需要設定定時執行某種活動。

。有四個按鈕,和四個lable,還有四個timer,他們之間一一對應。但單擊每一個按鈕時,過兩秒鐘自動修改對應的label的值。

 

代碼如下:

 

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都可以獨立運行,誰都不影響誰。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.