none
How to check if an application has stopped working in C#? RRS feed

  • Question

  • This is kind of a complicated problem; I'll try to explain it as clearly as I can.

    On one of the servers where I work, there lives a certain commercial application (as opposed to one developed in-house) which will sometimes throw a "This program has stopped working" error. When this happens, the application naturally needs to be restarted.

    We are trying to devise a method to monitor the application so we can be altered of this event when it occurs, instead of having to wait for users' error reports. My idea is to use a Windows service to monitor the application.

    I have tried using the below code to monitor the process in question:

    Process[] proc = Process.GetProcessesByName([process name]);
         bool isRunning = proc.Length > 0;
    
         if (!isRunning)
         {
              eventLog1.WriteEntry("Application does not appear to be running");
         }
         else if (logOK)
         {
              eventLog1.WriteEntry("Application appears to be running");
         }


    But all this can tell me is whether the application is running or not, and an application throwing the "stopped working" error is still running. Does anyone know how to listen for this error from C# code?

    To be clear, I am talking about creating a custom application to monitor a second, separate application that I did not write and over the source code of which I have no control.

    Monday, March 23, 2020 6:43 PM

Answers

  • You might look at see if you can work with Process.Responding. be forewarned this is not foolproof as there are many responses for an application to not respond e.g. heavily processing such as database inserts while the user interface is not responsive doesn't mean it's not working. 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by rmurrell Monday, March 23, 2020 8:26 PM
    Monday, March 23, 2020 7:08 PM
    Moderator

All replies

  • You might look at see if you can work with Process.Responding. be forewarned this is not foolproof as there are many responses for an application to not respond e.g. heavily processing such as database inserts while the user interface is not responsive doesn't mean it's not working. 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by rmurrell Monday, March 23, 2020 8:26 PM
    Monday, March 23, 2020 7:08 PM
    Moderator
  • Thanks for the quick response, Karen! I will definitely give this a try.

    For the benefit of anyone who finds this post later: we've also found that this application is throwing exception code 0xe0434352 according to Event Viewer, and this error code has received some pretty extensive troubleshooting coverage on other sites.

    We're going to attack that angle first, but this may very well help with further monitoring.

    Monday, March 23, 2020 8:26 PM