2013-01-08 16 views
0

我確實有一個檢查函數,一旦打開應用程序就會運行。 如何讓自動功能每20秒運行一次?c#函數使用定時器(AutoFunction)

Main() 
{ 
    Checking(); 
} 

public void Checking() // run this function every 20 seconds 
{ // some code here 
} 
+0

用一個定時器。將它的tick設置爲20Sec。 Claa你的函數定義了計時器tick事件 – Sami

+0

可能的重複[如何在.NET中每小時(或每小時特定時間間隔)提高事件?](http://stackoverflow.com/questions/307798/how-可以提高事件每小時或特定時間間隔每小時在中) –

+0

備註:如果代碼顯然與您不需要發佈的問題無關一些隨機的#@ $ @# - 空函數會很好。 –

回答

2

可以使用C#Timer類

public void Main() 
{ 
    var myTimer = new Timer(20000); 

    myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 

    myTimer.Enabled = true; 

    Console.ReadLine(); 
} 


private static void OnTimedEvent(object source, ElapsedEventArgs e) 
{ 
     Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); 
} 
0
Main() 
{ 
    Timer tm = new Timer(); 
    tm.Interval = 20000;//Milliseconds 
    tm.Tick += new EventHandler(tm_Tick); 
    tm.Start(); 
} 
void tm_Tick(object sender, EventArgs e) 
{ 
    Checking();  
} 

public void Checking() 
{ 
    // Your code 
}