【问题标题】:The service starts and then stops with message: The service did not respond to the start or control request in a timely fashion服务启动然后停止并显示消息:服务没有及时响应启动或控制请求
【发布时间】:2016-05-20 02:32:29
【问题描述】:

我使用以下代码开发了一个简单的 Windows 服务。 当我启动服务时,大约需要 20 秒,服务没有启动,并且我收到我在标题中提到的错误消息。

注意:它会在事件日志中记录“正在启动...”,但之后的代码似乎没有运行。请指教。

Dim eventId As Integer = 0

Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
    EventLog.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId)
    eventId = eventId + 1
End Sub

Protected Overrides Sub OnStart(ByVal CmdArgs() As String)

    EventLog.WriteEntry("Starting...") ' this line executes

    Dim timer As System.Timers.Timer = New System.Timers.Timer()
    timer.Interval = 60000 ' 60 seconds
    AddHandler timer.Elapsed, AddressOf Me.OnTimer
    timer.Start()
    Return

    ''''' some unused code below as I have Return
End Sub

【问题讨论】:

  • 感谢您的回复。我使用 Microsoft 示例更新了我的代码。现在它立即显示相同的消息。
  • 我不确定是什么问题。您的代码现在看起来不错,我刚刚将您的代码拉入了一个测试应用程序,它在本地对我来说很好。
  • 对我来说也不错。我仍在为此苦苦挣扎。还是谢谢你。

标签: vb.net windows-services


【解决方案1】:

Windows 为 OnStart 方法返回最多大约 30 秒的服务。如果不在此时间内,Windows 将假定服务未正确启动并为您提供错误。

您的OnStart 方法永远不会返回,因为您在其中有一个无限循环。您需要做的是在OnStart 方法中创建新线程,启动它,然后返回。新线程应该执行您希望服务实际执行的任何工作。

【讨论】:

    【解决方案2】:

    在对 SO 进行了更多研究后,我发现问题在于我使用的是 Debug 构建!!! See the post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2011-03-28
      • 2014-08-05
      • 2011-01-05
      • 1970-01-01
      相关资源
      最近更新 更多