C#
--
Questions
--
Followers
Top Experts
The issue is that we do not know when it stops and the business has to tell us that this or that isn't working and we then have to find the failing service (many boxes with the same service) and manually start it.
Writing a C# .NET app to "monitor" this service is what we are seeking but have no great examples of this. Â There are articles on how to keep alive the service that you built but not another service.
We are trying to "query" what is there and get it from that angle without much success.
Any suggestions? Â I know this isn't a windows service code below but it was easier to try and debug/output etc.
using System;
using System.Management;
using System.Collections.Generic
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RestartProcess
{
  class processes
  {
    public static void Main()
    {
      ManagementEventWatcher startWatch = new ManagementEventWatcher(
       new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"))
      startWatch.EventArrived += new EventArrivedEventHandler(s
      startWatch.Start();
      ManagementEventWatcher stopWatch = new ManagementEventWatcher(
       new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
      stopWatch.EventArrived += new EventArrivedEventHandler(s
      stopWatch.Start();
      Console.WriteLine("Press any key to exit");
      while (!Console.KeyAvailable) System.Threading.Thread.Sl
      startWatch.Stop();
      stopWatch.Stop();
    }
    static void stopWatch_EventArrived(obj
    {
      Console.WriteLine("Process
    }
    static void startWatch_EventArrived(ob
    {
      Console.WriteLine("Process
    }
  }
}
Is there a way to tell it "here is the service" to monitor and if it is down - restart it?
Any help would be greatly appreciated.
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
It seems illogical I agree....we went down this road first before trying to develop an app as a windows service.
My contention is if we wait x minutes and try then repeat until it starts back up we will be good to go. Â Give it a "lag" if you will.
So the idea is to have a file with the name of the service and wait time to try a restart to be successful.
Great thought though.
Does this describe the issue? Â If it does, then you are correct, simply enabling the service recovery will not work (unless the executable itself actually quits, if this is the case then you will want to ensure that you have checked the option to 'Enable actions for stops with errors.'
-saige-






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
If we give it a cyclic approach I am thinking this would resolve it. Â Like once the final "piece" has choked out it can be restarted. Â We have to do it manually but by the time the business knows it is down, we RDP into the box (after finding which one it is out of the 9 total servers) we have the ability to manually start it again.
So I was thinking about something like this:
using System;
using System.Management;
using System.Collections.Generic
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.ServiceProcess;
namespace RestartProcess
{
  class processes
  {
    public static void Main()
    {
      ManagementEventWatcher startWatch = new ManagementEventWatcher(
       new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"))
      startWatch.EventArrived += new EventArrivedEventHandler(s
      startWatch.Start();
      Console.WriteLine(startWat
      ManagementEventWatcher stopWatch = new ManagementEventWatcher(
       new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
      stopWatch.EventArrived += new EventArrivedEventHandler(s
      Console.WriteLine(stopWatc
      stopWatch.Start();
      Console.WriteLine("Press any key to exit");
      while (!Console.KeyAvailable) System.Threading.Thread.Sl
      startWatch.Stop();
      stopWatch.Stop();
    }
    static void stopWatch_EventArrived(obj
    {
      Console.WriteLine("Process
    }
    static void startWatch_EventArrived(ob
    {
      Console.WriteLine("Process
    }
    Â
    public static void StartService(String strSVCName)
    {
    ServiceController sc = new System.ServiceProcess.Serv
    try
    {
      if (sc.Status.Equals(ServiceC
      {
        WriteLogEntry(DateTime.Now
        sc.Start();
        sc.WaitForStatus(ServiceCo
        WriteLogEntry(DateTime.Now
      }
      if (sc.Status.Equals(ServiceC
      {
        WriteLogEntry(DateTime.Now
      }
      if (sc.Status.Equals(ServiceC
      {
        WriteLogEntry(DateTime.Now
      }
      if (sc.Status.Equals(ServiceC
      {
        WriteLogEntry(DateTime.Now
      }
      if (sc.Status.Equals(ServiceC
      {
        WriteLogEntry(DateTime.Now
      }
      if (sc.Status.Equals(ServiceC
      {
        WriteLogEntry(DateTime.Now
        sc.Start();
        sc.WaitForStatus(ServiceCo
        WriteLogEntry(DateTime.Now
      }
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.Inner
    }
    finally
    {
      sc.Close();
      sc.Dispose();
    }
    }
    public static void WriteLogEntry(DateTime When, String What)
    {
      StreamWriter WriteLog = new StreamWriter("ServiceUpkee
      WriteLog.Write(What + " " + When);
    }
  }
}
And then specify the strSVCName string to read from the flat file. Â So it would cycle through everything and if it has a status of Running nothing happens. Â Otherwise it writes to the log file and then restarts it and will wait until it is actually started.
Does that make sense?
I am just trying to determine the best way to get this into a windows service and "call it" to do the job since the console app can be closed, we could auto start the service, etc. for a better solution.
Which is of course the part that I am struggling with a bit here....lol
-saige-

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Thanks so much!






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
C#
--
Questions
--
Followers
Top Experts
C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).