Search code examples
vb.netmonitoringfreezewatchdog

How to programmatically check if an application has hang in VB.NET


I need to write a monitoring/watchdog program to check a series of application

The monitoring program should be able to

  1. Determine whether the applications it is monitoring has hang or no response
  2. If it hangs, restart the specific application

What sort of API in VB.NET can help me achieve this?

any code sample would be very helpful


Solution

  • You can use System.Diagnostics.Process to start/find the processes you're watching. Depending on the apps you're watching, you can use something like this:

    For Each proc As Process In System.Diagnostics.Process.GetProcesses
      If proc.ProcessName = "notepad" Then
        If proc.Responding = False Then
          ' attempt to kill the process
          proc.Kill()
    
          ' try to start it again
          System.Diagnostics.Process.Start(proc.StartInfo)
        End If
      End If
    Next
    

    Determining if an application is "hung" is not always clear. It may just be busy doing something. Also Process.Responding requires a MainWindow.

    That's a very simple example, but I hope it points you in the right direction.