VB.NET服务跳过子过程

时间:2014-01-10 18:39:22

标签: vb.net service vb.net-2010

我正在使用Visual Studio 2010在VB中编写Windows服务。该服务将每分钟左右检查数据库中表的更改。我有VB6的经验,但对VB.NET和创建服务都很陌生,所以我可能会忽略一些明显的东西。当服务停止,暂停,启动或恢复时,应该调用子过程。问题是程序跳过对子程序的调用。我在OnPause()和OnContinue()中设置了断点并逐步执行代码,以确认程序正在跳过对AgentStopped()和AgentStarted()的调用。我做错了什么?

Protected Overrides Sub OnPause()
    Log.WriteEntry("Agent paused.") 'This is written to the log
    AgentStopped() 'This line is skipped
End Sub

Protected Overrides Sub OnContinue()
    Log.WriteEntry("Agent restarted.") 'This is written to the log
    AgentStarted() 'This line is skipped
End Sub

Private Sub AgentStarted()
    Log.WriteEntry("AgentStarted called.") 'This never written to the log
End Sub

Private Sub AgentStopped()
    Log.WriteEntry("AgentStopped called.") 'This never written to the log
End Sub

1 个答案:

答案 0 :(得分:0)

如您所知,由于包含方法被称为,因此您无法跳过您所暗示的行。还有其他事情正在发生。我想你所展示的代码是简化的。正确?

我敢打赌,要么你正在运行的代码错误的版本错误,或者在你的实际代码的调用的方法内发生了异常

AgentStartedAgentStopped)。

为了调试这个,我会按照以下步骤操作:

  • 更改当前日志消息并在调用函数

    后添加消息

    受保护的覆盖Sub OnPause()     Log.WriteEntry(“代理暂停开始。”)     AgentStopped()     Log.WriteEntry(“代理暂停结束。”) 结束子

    受保护的覆盖Sub OnContinue()     Log.WriteEntry(“代理重启开始。”)     AgentStarted()     Log.WriteEntry(“代理重启结束。”) 结束子

    如果您看到新邮件,您将能够验证你实际上是运行错误部署的代码,而不是一个旧版本的正确版本。

  • 您应该对try - catch使用异常处理。我不是很确定(我从未使用它),但是使用UnhandledException事件获取可能未被捕获的异常信息可能会有所帮助。另一个选择是检查windows event log执行Web服务时发生的错误。

希望我帮忙!