2012-01-05 51 views
0

嗨我正在與Web應用程序與Windows.Forms.Timer。我創建Timer.Tick事件處理程序來處理Timer_Tick,但我不成功。我沒有得到任何錯誤,但我甚至無法得到結果。這是我的代碼Timer.Tick事件處理程序沒有得到事件Timer_Tick定時器

 System.Windows.Forms.Timer StopWatchTimer = new System.Windows.Forms.Timer(); 
    Stopwatch sw = new Stopwatch(); 

public void StopwatchStartBtn_Click(object sender, ImageClickEventArgs e) 
{ 
    StopWatchTimer.Enabled = true; 
    StopWatchTimer.Interval = 1; 
    StopWatchTimer.Start(); 
    this.StopWatchTimer.Tick += new EventHandler(StopWatchTimer1_Tick); 
    sw.Start(); 
} 


protected void StopWatchStopBtn_Click(object sender, ImageClickEventArgs e) 
{ 
    StopWatchTimer.Stop(); 
    sw.Reset(); 
    StopWatchLbl.Text = "00:00:00:000"; 
} 

public void StopWatchTimer1_Tick(object sender,EventArgs e) 
{ 
    TimeSpan elapsed = sw.Elapsed; 
    StopWatchLbl.Text = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", 
          Math.Floor(elapsed.TotalHours), 
          elapsed.Minutes, 
          elapsed.Seconds, 
          elapsed.Milliseconds); 
} 
+1

爲什麼使用WinForms Timer而不是System.Timers.Timer? – 2012-01-05 19:19:53

+0

什麼是StopWatchTimer vs sw? – 2012-01-05 19:21:18

+0

DeviantSeev我相信有答案,但只是爲了驗證StopWatchTimer和SW之間有什麼區別? – user1231231412 2012-01-05 19:21:27

回答

4

the MSDN documentation for Windows Forms Timer(重點煤礦):

實現可提高在用戶定義的時間間隔的事件的定時器。此計時器已針對Windows窗體應用程序進行了優化,並且必須在窗口中使用。

此計時器不適用於Web應用程序。您需要使用其他課程,例如System.Timers.Timer。然而,這有它自己的陷阱。

+0

你能告訴我如何給System.Timers.Timer打勾事件? – Hiren 2012-01-05 19:32:42

+0

很容易搜索,但在這裏你去:http://www.java2s.com/Code/CSharp/Development-Class/DemonstratesusingtheSystemTimersTimerclass2.htm – 2012-01-05 19:46:19

1

您是否嘗試在啓動計時器之前定義Tick事件?

this.StopWatchTimer.Tick += new EventHandler(StopWatchTimer1_Tick);  
StopWatchTimer.Start(); 
+0

好吧,我嘗試它,但它不工作 – Hiren 2012-01-05 19:23:31

+0

這是使用計時器的正確方法,但我錯過了部分地方您正嘗試將其用於Web應用程序。有關您的真實問題,請參閱其他答案。 – evasilchenko 2012-01-05 19:25:38

+0

OMG它是一個Web應用程序... Facepalm ... – 2012-01-05 19:29:55

1
public partial class TestFrom : Form 
{ 
    private Thread threadP; 
    private System.Windows.Forms.Timer Timer = new System.Windows.Forms.Timer(); 
    private string str; 

    public TestFrom() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Timer.Interval =100; 
     Timer.Tick += new EventHandler(TimeBussiness); 
     Timer.Enabled = true; 
     Timer.Start(); 
     Timer.Tag = "Start"; 
    } 

    void TimeBussiness(object sender, EventArgs e) 
    { 
     if (threadP.ThreadState == ThreadState.Running) 
     { 
      Timer.Stop(); 
      Timer.Tag = "Stop"; 
     } 
     else 
     { 
      //do my bussiness1; 
     } 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     ThreadStart threadStart = new ThreadStart(Salver); 
     threadP= new Thread(threadStart); 
     threadP.Start(); 
    } 

    private void Salver() 
    { 
     while (Timer.Tag == "Stop") 
     { 

     } 
     //do my bussiness2; 
     Timer.Start(); 
     Timer.Tag = "Start"; 
    } 
}