【问题标题】:Windows Service keeps starting and stoppingWindows 服务不断启动和停止
【发布时间】:2014-02-10 10:48:47
【问题描述】:

在启动我的 Windows 服务时不断遇到此错误消息。

本地计算机上的服务启动然后停止。如果某些服务没有被其他服务和程序使用,它们会自动停止。

我的代码:

     protected override void OnStart(string[] args)
     {
        string eventLogMessage = string.Format(@"Notify Service is starting :{0}", DateTime.Now);
        EventLogging.LogInformation(eventLogMessage);

            double interval;

            try
            {
                interval = Convert.ToDouble(ConfigurationManager.AppSettings["intervalInSeconds"]);
                EventLogging.LogInformation(
                    string.Format("Loaded configuration: Interval duration is {0} minutes", (interval / 60)));
            }
            catch (Exception exception)
            {
                interval = 3600;

                eventLogMessage = string.Format("Loading configuration failed: Interval duration is {0} minutes", (interval / 60));
                eventLogMessage += string.Format("\nMessage was: {0}", exception.Message);

                EventLogging.LogWarning(eventLogMessage);
            }

            interval = interval * 1000;

            _timer.Interval = interval;
            _timer.Elapsed += TimerTick;
            _timer.Start();

            eventLogMessage = string.Format(@"Notify service has started: {0}", DateTime.Now);
            EventLogging.LogInformation(eventLogMessage);

            var workerThread = new Thread(NotifyUsers) { IsBackground = true };
            workerThread.Start();

    }

    private void NotifyUsers()
    {
        var userBL = new UserBL();

        List<User> usersToBeMailed = userBL.GetAllUsersWhosePasswordsWillExpire();

        string eventLogMessage = string.Format("Number of users to be mailed is {0}", usersToBeMailed.Count);
        EventLogging.LogInformation(eventLogMessage);

        foreach (User user in usersToBeMailed)
        {
            userBL.MailUser(user);
        }
    }

    private void TimerTick(object sender, ElapsedEventArgs e)
    {
        var workerThread = new Thread(NotifyUsers) { IsBackground = true };
        workerThread.Start();
    }

    protected override void OnStop()
    {
        base.OnStop();

        string eventLogMessage = @"Password notify service has stopped: " + DateTime.Now;

        EventLogging.LogInformation(eventLogMessage);
    }


    protected override void OnPause()
    {
        base.OnPause();
        _timer.Stop();
        EventLogging.LogWarning("Paused");
    }

    protected override void OnContinue()
    {
        base.OnContinue();
        _timer.Start();
        EventLogging.LogInformation("Resumed");
    }
}

【问题讨论】:

  • 对于OnStart`OnStop` 方法的内容有相当严格的规定。我猜是workerThread 代码。作为测试尝试删除它,看看它是否仍然停止。
  • 还将 _timer 和 worker 移至单独的“初始化”方法。 OnStart 应该很简单,具有完整的异常处理。
  • 尝试将调试器附加到服务以查找发生异常的位置,在 OnStart() 方法的第一行添加 Thread.Sleep(30000) 以便您有时间附加代码完成执行之前的调试器。不要忘记调用 Thread.Sleep 后的断点,这样你可以在线程唤醒时手动逐行调试。
  • 您可能会在 windows 日志中找到一些有用的信息。在事件查看器上打开 MMC,导航到 Windows 日志-> 应用程序。通常你会在那里发现一些抛出的异常。

标签: c# windows-services


【解决方案1】:

您必须至少有一个 您的 线程在做某事而不是后台线程,以保持进程处于活动状态。 OnStart 代码运行所在的线程实际上并不是您的线程之一,此外,您必须从 OnStart 返回才能被视为已成功启动。

这可以像从您的 OnStart 方法创建和运行一个新的 Thread 一样简单,该方法只等待您从 OnStop 代码中的 SetManualResetEvent 对象。

另外,找一些有用的工作让这个线程去做(但仍然使用事件对象来发出信号,什么时候应该关闭),或者另一种选择是 - 考虑这段代码是否属于服务。如果它只是定期唤醒以通知用户,为什么不使用计划任务呢?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多