如何停止 windows 服务中的计时器,以便代码不会多次命中?
How to stop timer in windows service so that code doesn't hit multiple times?
这是示例控制台应用程序,不是原始 WINDOWS 服务
我有以下代码(仅示例代码)。我希望我的计时器只执行一次,然后停止。
目前我正在使用 count
字段来检查代码是否已被命中。它工作正常,但我想优化这些东西。
注意:
使用计时器的原因是 windows 服务安装期间 OnStart
事件不应延迟服务安装过程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class Program
{
static int count = 0;
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += OnElapseTime;
timer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
timer.Start();
System.Threading.Thread.Sleep(30000);
timer.Stop();
}
static void OnElapseTime(object sender,
System.Timers.ElapsedEventArgs e)
{
if (count < 1)
{
count++;
Console.WriteLine("Timer ticked...");
Console.ReadLine();
}
}
}
}
有多种方式可以达到你想要的效果,本质上是延迟执行。
就修改您所拥有的内容而言,最简单的可能是这样的:
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += OnElapseTime;
timer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
timer.Start();
}
static void OnElapseTime(object sender, System.Timers.ElapsedEventArgs e)
{
var timer = (System.Timers.Timer)sender;
timer.Stop(); // stop the timer after first run
// do the work.. (be careful, this is running on a background thread)
Console.WriteLine("Timer ticked...");
Console.ReadLine();
}
Antoher 方法可能是使用 System.Threading.Timer class 而不是 System.Timers.Timer 并安排一次延迟执行:
static void Main()
{
var timer = new System.Threading.Timer(RunDelayed, null, imeSpan.FromSeconds(5).TotalMilliseconds, Timeout.Infinite );
}
private void Callback( Object state )
{
// do the work.. (be careful, this is running on a background thread)
Console.WriteLine("Timer ticked...");
Console.ReadLine();
}
这是一篇很好的文章,讨论了 .NET 中可用的不同计时器 class 之间的区别:https://web.archive.org/web/20150329101415/https://msdn.microsoft.com/en-us/magazine/cc164015.aspx
只需将 AutoReset 设置为 false
。这会导致事件仅触发一次。您可以使用 count
.
删除代码
timer.AutoReset = false;
这是示例控制台应用程序,不是原始 WINDOWS 服务
我有以下代码(仅示例代码)。我希望我的计时器只执行一次,然后停止。
目前我正在使用 count
字段来检查代码是否已被命中。它工作正常,但我想优化这些东西。
注意:
使用计时器的原因是 windows 服务安装期间 OnStart
事件不应延迟服务安装过程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class Program
{
static int count = 0;
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += OnElapseTime;
timer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
timer.Start();
System.Threading.Thread.Sleep(30000);
timer.Stop();
}
static void OnElapseTime(object sender,
System.Timers.ElapsedEventArgs e)
{
if (count < 1)
{
count++;
Console.WriteLine("Timer ticked...");
Console.ReadLine();
}
}
}
}
有多种方式可以达到你想要的效果,本质上是延迟执行。
就修改您所拥有的内容而言,最简单的可能是这样的:
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += OnElapseTime;
timer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
timer.Start();
}
static void OnElapseTime(object sender, System.Timers.ElapsedEventArgs e)
{
var timer = (System.Timers.Timer)sender;
timer.Stop(); // stop the timer after first run
// do the work.. (be careful, this is running on a background thread)
Console.WriteLine("Timer ticked...");
Console.ReadLine();
}
Antoher 方法可能是使用 System.Threading.Timer class 而不是 System.Timers.Timer 并安排一次延迟执行:
static void Main()
{
var timer = new System.Threading.Timer(RunDelayed, null, imeSpan.FromSeconds(5).TotalMilliseconds, Timeout.Infinite );
}
private void Callback( Object state )
{
// do the work.. (be careful, this is running on a background thread)
Console.WriteLine("Timer ticked...");
Console.ReadLine();
}
这是一篇很好的文章,讨论了 .NET 中可用的不同计时器 class 之间的区别:https://web.archive.org/web/20150329101415/https://msdn.microsoft.com/en-us/magazine/cc164015.aspx
只需将 AutoReset 设置为 false
。这会导致事件仅触发一次。您可以使用 count
.
timer.AutoReset = false;