【问题标题】:c# Function work with Timer(AutoFunction)c# Function 与 Timer(AutoFunction) 一起使用
【发布时间】:2012-12-21 21:01:33
【问题描述】:

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

Main()
{
  Checking();
}

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

【问题讨论】:

标签: c# function automation


【解决方案1】:

您可以使用 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);
}

【讨论】:

    【解决方案2】:
    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
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多