c#功能与定时器一起工作(自动功能)
我有一个检查功能,一旦打开应用程序就会运行。 如何使其自动运行,如每20秒运行一次功能c#功能与定时器一起工作(自动功能),c#,function,automation,C#,Function,Automation,我有一个检查功能,一旦打开应用程序就会运行。 如何使其自动运行,如每20秒运行一次功能 Main() { Checking(); } public void Checking() // run this function every 20 seconds { // some code here } 您可以使用C#Timer类 public void Main() { var myTimer = new Timer(20000); myTimer.Elapsed
Main()
{
Checking();
}
public void Checking() // run this function every 20 seconds
{ // some code here
}
您可以使用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);
}
使用计时器。将其刻度设置为20秒。Claa计时器滴答事件定义中的函数可能与旁注重复:如果代码显然与问题无关,则无需发布一些随机的#@$@-空函数即可。
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
}