更新时间:2023-12-02 16:14:52
使用System.Timers.Timer
您有更多选项,其中System.Threading.Timer
是一个轻量级计时器。我建议你使用System.Timers.Timer
试试这个...
WithSystem.Timers.Timer
you have more options, whereSystem.Threading.Timer
is a lightweight timer. I would recommend you to useSystem.Timers.Timer
Try this ...
using System.Timers;
Timer tmrExecutor = new Timer();
protected override void OnStart(string[] args)
{
tmrExecutor.Elapsed += new ElapsedEventHandler(tmrExecutor_Elapsed); // adding Event
tmrExecutor.Interval = 5000; // Set your time here
tmrExecutor.Enabled = true;
tmrExecutor.Start();
}
private void tmrExecutor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//Do your work here
}
protected override void OnStop()
{
tmrExecutor.Enabled = false;
}