C# Win服务中的计时器不调用已用事件
我正在编写Windows服务,该服务在定义的时间段(现在是20秒)后调用该方法。一切都很好,直到没有。似乎找不到问题的原因。 服务似乎正确地启动和停止,并提供日志条目,但它似乎不调用已过去的事件C# Win服务中的计时器不调用已用事件,c#,timer,C#,Timer,我正在编写Windows服务,该服务在定义的时间段(现在是20秒)后调用该方法。一切都很好,直到没有。似乎找不到问题的原因。 服务似乎正确地启动和停止,并提供日志条目,但它似乎不调用已过去的事件 public partial class UssPwdSyncService : ServiceBase { private Timer timer1 = null; public UssPwdSyncService() { InitializeComponen
public partial class UssPwdSyncService : ServiceBase
{
private Timer timer1 = null;
public UssPwdSyncService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
timer1 = new Timer();
this.timer1.Interval = 20000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_tick);
timer1.Enabled = true;
LogHandling.WriteErrorLogs("Service has started! LOg!");
}
catch (Exception ex)
{
LogHandling.WriteErrorLogs(ex);
}
}
private void timer1_tick(object sender, ElapsedEventArgs e)
{
ConfigurationManager.RefreshSection("connectionStrings");
foreach (ConnectionStringSettings cStr in ConfigurationManager.ConnectionStrings)
{
string name = cStr.Name;
string connString = cStr.ConnectionString;
string provider = cStr.ProviderName;
LogHandling.WriteErrorLogs(name + " " + connString + " " + provider);
}
LogHandling.WriteErrorLogs("This does something!");
}
protected override void OnStop()
{
timer1.Enabled = false;
LogHandling.WriteErrorLogs("Service has stoped!");
}
}
有人能指出我遗漏了什么吗?我想你正在使用
系统中的计时器。计时器。您需要调用计时器的Start()
方法。我将try-catch移到了timer1\u-tick方法。
这是进行异常检查的正确位置
您可能会抛出一个异常o timer1\u tick代码。
您有连接字符串部分吗
注意:我更喜欢使用Start
和Stop
方法,而不是Enabled=true
并且已启用=false
。
有两种方法是正确的
试试这个:
public partial class UssPwdSyncService : ServiceBase
{
private Timer timer1 = null;
public UssPwdSyncService()
{
InitializeComponent();
this.timer1 = new Timer();
this.timer1.Interval = 20000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_tick);
}
protected override void OnStart(string[] args)
{
this.timer1.Start();
LogHandling.WriteErrorLogs("Service has started! LOg!");
}
private void timer1_tick(object sender, ElapsedEventArgs e)
{
try
{
ConfigurationManager.RefreshSection("connectionStrings");
foreach (ConnectionStringSettings cStr in ConfigurationManager.ConnectionStrings)
{
string name = cStr.Name;
string connString = cStr.ConnectionString;
string provider = cStr.ProviderName;
LogHandling.WriteErrorLogs(name + " " + connString + " " + provider);
}
LogHandling.WriteErrorLogs("This does something!");
}
catch (Exception ex)
{
LogHandling.WriteErrorLogs(ex);
}
}
protected override void OnStop()
{
this.timer1.Stop();
LogHandling.WriteErrorLogs("Service has stoped!");
}
}
根据文档,您还可以通过将Enabled设置为true来开始计时。的确我在电视上看到的更常见的“bug”是人们将启用设置为true
并调用Start()
。(它不会产生错误的行为,但它是多余的)正确的。调用start方法,但要看到它们(start()和Enabled)做相同的事情。有些人喜欢方法,有些人喜欢属性。事件会触发吗?还是只有一次?如果后者尝试将AutoReset设置为true。@PaulF-默认的AutoReset
是true
,你可以看到他们new
正在调高正上方的计时器。@Damien\u不信者:你是对的,我误读了文档。如果你在timer1\u tick
的第一行放一个日志行,它会被记录吗?可能它正在启动,但ConfigurationManager.RefreshSection
或您的foreach
引发异常。您更改了什么?你为什么相信它会解决问题?“试试这个”不是一个答案。对此的解释??我补充到了答案中。我相信这个错误是在计时。然后他不记录日志,因为在记录之前会抛出异常。是的,这就是抛出异常的地方。我找错地方了。很傻,但是谢谢!