2011-04-09 20 views
1

我想定時器從0到3運行一個計數器。我添加了計時器從視覺基礎2008中的工具箱(而不是創建一個對象和使用屬性),你可以看到定時器在底部的winform ..定時器(winforms)

 int timerCounter; 
    private void animationTimer_Tick(object sender, EventArgs e) 
    { 
     //timer should go 0,1,2,3..and then reset 
     while (true) 
     { 
      timerCounter++; 
      if (timerCounter > 3) 
      { 
       timerCounter = 0; 
      } 
      game.Twinkle(); 
      //screen gets repainted. 
      Refresh(); 
     } 



    } 

定時器能工作嗎? (我啓用它並將其設置爲33毫秒)

+0

賠率約爲99%。試着問下一個可以可靠解答的問題。 – 2011-04-09 16:52:40

+0

你爲什麼不自己嘗試一下? 「做功」是什麼意思?什麼對你很重要?如何知道它是否有效?精確性很重要,aso。 – vidstige 2011-04-09 16:52:58

+0

問題是我有一半的代碼工作..這是一小塊大代碼..我試圖避免數小時的調試 – 2011-04-09 17:11:31

回答

1

將計時器的間隔設置爲1000(即1000ms或1秒)。然後當你啓用它時,它會一直繼續,每當它經過間隔時觸發timer1_tick事件。

下面是關於如何做到這一點的例子:

int count = 0; 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    count++; 

    if (count == 3) 
    { 
     //Do something here, because it's the third toll of the bell. 

     //But also reset the counter after you're done. 
     count = 0; 
    } 
} 

不要忘記.Enable計時器!

+0

我不需要while循環嗎?... – 2011-04-09 17:14:01

+0

@Dmitry:不,一旦定時器被啓用,它將繼續在表單上運行,直到你使用'.Enabled = false'。 – 2011-04-09 17:18:50

+0

好的。 chrystal清晰。謝謝 – 2011-04-09 17:20:09