C# 如何用計時器Timer控件實現停留幾秒再做切換窗體的操作


C# Timer用法及實例詳解

關於C# Timer類  在C#里關於定時器類就有3個

C# Timer使用的方法1.定義在System.Windows.Forms里

C# Timer使用的方法2.定義在System.Threading.Timer類里  "

C# Timer使用的方法3.定義在System.Timers.Timer類里

◆System.Windows.Forms.Timer

應用於WinForm中的,它是通過Windows消息機制實現的,類似於VB或Delphi中的Timer控件,內部使用API  SetTimer實現的。它的主要缺點是計時不精確,而且必須有消息循環,Console  Application(控制台應用程序)無法使用。   
  
◆System.Timers.Timer

和System.Threading.Timer非常類似,它們是通過.NET  Thread  Pool實現的,輕量,計時精確,對應用程序、消息沒有特別的要求。

◆System.Timers.Timer還可以應用於WinForm,完全取代上面的Timer控件。它們的缺點是不支持直接的拖放,需要手工編碼。

C# Timer用法實例

使用System.Timers.Timer類

 

1
2
3
4
5
6
7
8
9
System.Timers.Timer t =  new  System.Timers.Timer(10000);    //實例化Timer類,設置間隔時間為10000毫秒;   
     t.Elapsed +=  new  System.Timers.ElapsedEventHandler(theout);  //到達時間的時候執行事件;   
     t.AutoReset =  true ;    //設置是執行一次(false)還是一直執行(true);   
     t.Enabled =  true ;      //是否執行System.Timers.Timer.Elapsed事件;   
     
     public  void  theout( object  source, System.Timers.ElapsedEventArgs e)   
      {   
         MessageBox.Show( "OK!" );   
      }
 

xiangjuan314 

采納率:95% 來自團隊:電腦網絡行家 擅長: 佛山市 煩惱 電腦/網絡 生活 文化/藝術

其他回答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;
 
namespace  WindowsFormsApplication1
{
     public  partial  class  Form1 : Form
     {
         int  a = 0;
         public  Form1()
         {
             InitializeComponent();
         }
 
         private  void  button1_Click( object  sender, EventArgs e)
         {
             timer1.Enabled =  true ;
         }
         
         private  void  timer1_Tick( object  sender, EventArgs e)
         {
             a += 1;
             progressBar1.Value = a;
             
             if  (a == 5) {
                 Form2 frm =  new  Form2();
                 timer1.Enabled =  false ;
                 a = 0;
                 frm.Show();
                 this .Hide ();
              }
         }
 
         private  void  Form1_Load( object  sender, EventArgs e)
         {
             progressBar1.Maximum = 5;
             progressBar1.Minimum =0;
             progressBar1.Value = 0;
             timer1.Interval = 1000;
         }
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM