使用一项服务来阻止另一项服务

时间:2015-11-16 19:31:20

标签: visual-studio windows-services

我编写了一个Windows服务应用程序,其目的是停止并稍后启动另一项服务。

当我尝试停止其他服务时,我收到此错误。

StopTheProcess失败无法在计算机上停止CaseMixProcessManager服务'。'。System.ComponentModel.Win32Exception(0x80004005):此时服务无法接受控制消息

我可以看到我试图停止的服务状态是4运行。用户具有停止和启动服务的权限。

我仍然没有得到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

1 个答案:

答案 0 :(得分:0)

我想出了问题,我想让这个帖子的未来读者了解它。

哈利约翰斯顿在正确的轨道上建议了一个我以前从未听过的单独的话题。但该线程属于服务我试图阻止不停止的服务。因为On Start例程从未结束,所以我可以以编程方式停止该过程。我把我正在做的大部分内容放在一个单独的线程中。