c#函数与Timer(AutoFunction)一起工作

时间:2013-01-08 03:18:01

标签: c# function automation

我有一个检查功能,一旦打开应用程序就会运行。 如何让它像每20秒运行一次自动功能?

Main()
{
  Checking();
}

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

2 个答案:

答案 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);
}

答案 1 :(得分: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
}