使用一项服务停止另一项服务
Using One Service to Stop Another
我编写了一个 windows 服务应用程序,其目的是停止并稍后启动另一个服务。
当我尝试停止其他服务时出现此错误。
StopTheProcess 失败无法停止计算机“.”上的 CaseMixProcessManager 服务。System.ComponentModel.Win32Exception (0x80004005): 该服务此时无法接受控制消息
我可以看到 运行 我试图停止的服务状态为 4。用户有停止和启动服务的权限。
我仍然不明白 Harry 的建议,所以我发布了整个程序,因为它没有那么多。也许 Harry 或其他人可以建议如何更改它。
Public Sub StartProcessing()
Try
ClassLibraries.clsUtilities.LogSource = "ServiceController"
ClassLibraries.clsUtilities.WriteLog("Service Controller Started " & cGlobals.m_Version, EventLogEntryType.Information)
m_StartHour = GetConfiguation("StartHour")
m_StopHour = GetConfiguation("StopHour")
m_ServiceName = GetConfiguation("ServiceName")
m_Service = New ServiceProcess.ServiceController(m_ServiceName)
If StartOrStop() = True Then
WaitToStart()
Else
WaitToStop()
End If
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in Start Processing " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
Private Function FigureNextTime(p_Hour As String) As Boolean
Dim _TimeToRestart As DateTime
Dim _TodayString As String
Dim _RestartString As String
Dim _NowTime As DateTime
Dim _Result As Int32
Try
ClassLibraries.clsUtilities.WriteLog("FigureNextTime " & p_Hour, EventLogEntryType.Information)
' we need to restart the process automatically at a certain hour
If p_Hour = "N" Then
Return False
End If
If IsNumeric(p_Hour) = False Then
Return False
End If
p_Hour = p_Hour & ":00:00"
_TodayString = Convert.ToString(DateTime.Today)
_RestartString = _TodayString.Replace("12:00:00", p_Hour)
_RestartString = _RestartString.Replace("AM", "")
_TimeToRestart = Convert.ToDateTime(_RestartString)
' next compare the two dates see if the time has passed
_NowTime = (DateTime.Now)
_Result = DateTime.Compare(_NowTime, _TimeToRestart)
If (_Result >= 0) Then
' we add one day to the restart time
_TimeToRestart = _TimeToRestart.AddDays(1)
End If
m_MinutesDifference = DateDiff(DateInterval.Minute, _NowTime, _TimeToRestart)
' figure the millseconds until it's time to start the process
m_MilliSeconds = m_MinutesDifference * 60000
Return True
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in FigureNextTime " & ex.Message, EventLogEntryType.Error)
Return False
End Try
End Function
Private Function GetConfiguation(p_KeyName As String) As String
Return ConfigurationManager.AppSettings(p_KeyName)
End Function
Private Sub WaitToStart()
Try
ClassLibraries.clsUtilities.WriteLog("WaitToStart", EventLogEntryType.Information)
If FigureNextTime(m_StartHour) = False Then
Exit Sub
End If
ClassLibraries.clsUtilities.WriteLog("Will start in " & m_MinutesDifference & " Minutes", EventLogEntryType.Information)
m_svcTimer = New System.Timers.Timer(m_MilliSeconds)
' Hook up the Elapsed event for the timer.
AddHandler m_svcTimer.Elapsed, AddressOf StartTheProcess
' Set the Interval
m_svcTimer.Interval = m_MilliSeconds
m_svcTimer.Enabled = True
m_svcTimer.Start()
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in WaitToStart " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
我解决了这个问题,我想让这个帖子的未来读者知道。
Harry Johnston 的建议是正确的,我以前从未听说过一个单独的线程。但是线程属于我试图停止的服务,而不是正在停止的服务。因为 On Start 例程从未结束,所以我可以通过编程方式停止该过程。我将我正在做的大部分工作放在一个单独的线程中。
我编写了一个 windows 服务应用程序,其目的是停止并稍后启动另一个服务。
当我尝试停止其他服务时出现此错误。
StopTheProcess 失败无法停止计算机“.”上的 CaseMixProcessManager 服务。System.ComponentModel.Win32Exception (0x80004005): 该服务此时无法接受控制消息
我可以看到 运行 我试图停止的服务状态为 4。用户有停止和启动服务的权限。
我仍然不明白 Harry 的建议,所以我发布了整个程序,因为它没有那么多。也许 Harry 或其他人可以建议如何更改它。
Public Sub StartProcessing()
Try
ClassLibraries.clsUtilities.LogSource = "ServiceController"
ClassLibraries.clsUtilities.WriteLog("Service Controller Started " & cGlobals.m_Version, EventLogEntryType.Information)
m_StartHour = GetConfiguation("StartHour")
m_StopHour = GetConfiguation("StopHour")
m_ServiceName = GetConfiguation("ServiceName")
m_Service = New ServiceProcess.ServiceController(m_ServiceName)
If StartOrStop() = True Then
WaitToStart()
Else
WaitToStop()
End If
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in Start Processing " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
Private Function FigureNextTime(p_Hour As String) As Boolean
Dim _TimeToRestart As DateTime
Dim _TodayString As String
Dim _RestartString As String
Dim _NowTime As DateTime
Dim _Result As Int32
Try
ClassLibraries.clsUtilities.WriteLog("FigureNextTime " & p_Hour, EventLogEntryType.Information)
' we need to restart the process automatically at a certain hour
If p_Hour = "N" Then
Return False
End If
If IsNumeric(p_Hour) = False Then
Return False
End If
p_Hour = p_Hour & ":00:00"
_TodayString = Convert.ToString(DateTime.Today)
_RestartString = _TodayString.Replace("12:00:00", p_Hour)
_RestartString = _RestartString.Replace("AM", "")
_TimeToRestart = Convert.ToDateTime(_RestartString)
' next compare the two dates see if the time has passed
_NowTime = (DateTime.Now)
_Result = DateTime.Compare(_NowTime, _TimeToRestart)
If (_Result >= 0) Then
' we add one day to the restart time
_TimeToRestart = _TimeToRestart.AddDays(1)
End If
m_MinutesDifference = DateDiff(DateInterval.Minute, _NowTime, _TimeToRestart)
' figure the millseconds until it's time to start the process
m_MilliSeconds = m_MinutesDifference * 60000
Return True
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in FigureNextTime " & ex.Message, EventLogEntryType.Error)
Return False
End Try
End Function
Private Function GetConfiguation(p_KeyName As String) As String
Return ConfigurationManager.AppSettings(p_KeyName)
End Function
Private Sub WaitToStart()
Try
ClassLibraries.clsUtilities.WriteLog("WaitToStart", EventLogEntryType.Information)
If FigureNextTime(m_StartHour) = False Then
Exit Sub
End If
ClassLibraries.clsUtilities.WriteLog("Will start in " & m_MinutesDifference & " Minutes", EventLogEntryType.Information)
m_svcTimer = New System.Timers.Timer(m_MilliSeconds)
' Hook up the Elapsed event for the timer.
AddHandler m_svcTimer.Elapsed, AddressOf StartTheProcess
' Set the Interval
m_svcTimer.Interval = m_MilliSeconds
m_svcTimer.Enabled = True
m_svcTimer.Start()
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in WaitToStart " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
我解决了这个问题,我想让这个帖子的未来读者知道。
Harry Johnston 的建议是正确的,我以前从未听说过一个单独的线程。但是线程属于我试图停止的服务,而不是正在停止的服务。因为 On Start 例程从未结束,所以我可以通过编程方式停止该过程。我将我正在做的大部分工作放在一个单独的线程中。