Service that monitors another application

gpstefansson

New member
Joined
Mar 1, 2006
Messages
1
Programming Experience
1-3
Hi all,

I have a service that monitors another application every 20 seconds and restarts it if the application is "not responding" 3 times in a row.

This seems to be working to some extent but when the application becomes "not responding", my service goes through this cycle of restarting the application as much as ten times. For example:

1. Service reads status of app as not responding three times in a row.
2. Restarts application.
3. 20 seconds later the service starts again checking the application and finds it to be not responding three times in a row so it restarts the app again.

This can go on for 10 minutes. If you have any ideas it would be great to hear them.

Cheers, G

Here is the code for the service:
VB.NET:
Private Sub CheckApp()
        Dim myProcesses() As Process = Nothing
        Dim strEv As String = String.Empty
        Try
            If isBusy Then Exit Sub
            isBusy = True

            Responding(0) = Responding(1)
            Responding(1) = Responding(2)

            ' Link dynamically to process and check status. 
            myProcesses = Process.GetProcessesByName("AppName")
           
            Select Case myProcesses.GetLength(0)
                Case 0
                    'No copy of application running. Let's start one
                    Process.Start(myAppName)
                    Responding(2) = True
                    Responding(1) = True ' Restet Responding
                    Responding(0) = True

                Case 1
                    ' One copy running. Let's check it.
                    myProcesses(0).Refresh()
                    If Not myProcesses(0).Responding Then
                        ' Forces the process to close if the Responding value is False.
                        Responding(2) = False
                        If Responding(0) = False AndAlso Responding(1) = False AndAlso Responding(2) = False Then
                            myProcesses(0).Kill()
                            Threading.Thread.Sleep(4000) ' Wait 4 sec
                            Process.Start(myAppName)
                            Responding(2) = True
                            Responding(1) = True ' Reset Responding
                            Responding(0) = True
                        End If
                    ElseIf myProcesses(0).HasExited Then
                        Process.Start(myAppName)
                        Responding(2) = True
                        Responding(1) = True ' Reset Responding
                        Responding(0) = True
                    Else
                        ' AppName is responding.
                        Responding(2) = True
                    End If

                Case Else
                    ' More than one copy running. 
                    Dim instance As Process
                    For Each instance In myProcesses
                        instance.Kill()
                        Threading.Thread.Sleep(3000) ' Wait 3 sec
                    Next
                    instance = Nothing
                    Process.Start(myAppName)
                    Responding(2) = True
                    Responding(1) = True ' Reset Responding
                    Responding(0) = True
              End Select

  Catch e As Exception
            EventLog1.WriteEntry(Now() & ": " & e.Message)
        Finally
            myProcesses = Nothing
            isBusy = False
        End Try
End Sub
Start of myService class:
VB.NET:
Public Class myService
    Public Timer1 As New System.Timers.Timer()
    Public myAppName As ProcessStartInfo
    Public Responding As Boolean() = {True, True, True}
    Public isBusy As Boolean = False

Protected Overrides Sub OnStart(ByVal args() As String)
        Try
            EventLog1.WriteEntry(Now() & " In OnStart")

            myAppName = New ProcessStartInfo(strPath) ' strPath = string path to appication.
            myAppName.WindowStyle = ProcessWindowStyle.Normal
            Process.Start(myAppName)

            AddHandler Timer1.Elapsed, AddressOf OnTimerElapsed
            Timer1.Interval = 20000 ' 20 sek
            Timer1.Enabled = True
            Timer1.Start()
        Catch e As Exception
            EventLog1.WriteEntry(Now() & ": " & e.Message & " Error in OnStart!")
        End Try
End Sub

Private Sub OnTimerElapsed(ByVal source As Object, ByVal e As Timers.ElapsedEventArgs)
        CheckApp()
End Sub
 
Back
Top