Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Win服务中的计时器不调用已用事件_C#_Timer - Fatal编程技术网

C# Win服务中的计时器不调用已用事件

C# Win服务中的计时器不调用已用事件,c#,timer,C#,Timer,我正在编写Windows服务,该服务在定义的时间段(现在是20秒)后调用该方法。一切都很好,直到没有。似乎找不到问题的原因。 服务似乎正确地启动和停止,并提供日志条目,但它似乎不调用已过去的事件 public partial class UssPwdSyncService : ServiceBase { private Timer timer1 = null; public UssPwdSyncService() { InitializeComponen

我正在编写Windows服务,该服务在定义的时间段(现在是20秒)后调用该方法。一切都很好,直到没有。似乎找不到问题的原因。 服务似乎正确地启动和停止,并提供日志条目,但它似乎不调用已过去的事件

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
引发异常。您更改了什么?你为什么相信它会解决问题?“试试这个”不是一个答案。对此的解释??我补充到了答案中。我相信这个错误是在计时。然后他不记录日志,因为在记录之前会抛出异常。是的,这就是抛出异常的地方。我找错地方了。很傻,但是谢谢!