Я написал приложение для работы с Windows, целью которого является остановка, а затем запуск другой службы.Использование одной службы для остановки еще
Я получаю эту ошибку, когда пытаюсь остановить другую службу. «».
Ошибка в StopTheProcess Не удается остановить службу CaseMixProcessManager на компьютере System.ComponentModel.Win32Exception (0x80004005): Служба не может принимать управляющие сообщения в это время
Я могу видеть, что статус службы я нахожусь попытка остановить - 4 для запуска. У пользователя есть привилегии для остановки и запуска службы.
Я до сих пор не понимаю, что предлагает Гарри, поэтому я публикую всю программу, так как это не так много. Возможно, Гарри или кто-то другой может предложить, как изменить его.
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
Предполагая, что 'sc stop' работает в целевой службе, наиболее вероятной проблемой является то, что вы не можете запускать или останавливать службу из процедуры запуска или остановки другой службы. Попробуйте сделать запрос на остановку из отдельного потока. –
Гарри, Что ты имеешь в виду, когда говоришь из отдельной темы? –
Из потока, который вы создали сами, а не поток, который вызывает OnStart() или OnStop(). –