-1

I'm writing a simple watchdog application that will start and stop another application I'm writing based on whether the third application is running or not. in other words, if application A is running then start application B. When application A stops, stop application B.

the problems is that my watchdog keeps stopping application B and immediately restarts it.

here is what I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using WindowScrape.Types;

namespace ConnectAndWait
{
    class CheckForApplication
    {

        public static System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
        public static bool goodtogo;
        public static void checking()
        {
            _timer.Interval = 3000;
            _timer.Tick += _timer_Tick;
            _timer.Start();
        }

        public static void _timer_Tick(object sender, EventArgs e)
        {
            Process[] myprocess = Process.GetProcessesByName("ApplicationA");
            Process[] proc = Process.GetProcessesByName("notepad");
            if (myprocess.Length == 0)
            {
               goodtogo = false;
            }
            else
            {
                var win = Process.GetProcessesByName("ApplicationA");
                var mainform = NativeMethods.FindWindow("TMainForm_ihm", null);
                var children = NativeMethods.FindWindowEx(mainform, IntPtr.Zero, "TRzPageControl", null);
                var final = NativeMethods.FindWindowEx(children, IntPtr.Zero, "TRzTabSheet", "Operation" );

                if (final.ToString() != "0")
                {
                    goodtogo = true;
                }
                else
                {
                    goodtogo = false;
                }
            }
            if (goodtogo == true)
            {

                if (proc.Length == 0)
                {
                    Process.Start("notepad.exe");
                    MessageBox.Show("notepad started");
                }
            }
            else if (goodtogo == false)
            {
            if (proc.Length != 0)
            {
                proc[0].Kill();
                MessageBox.Show("process killed"); // <-- This never gets fired
                // as long as application A keeps running.  At first I thought I was stopping it
                // with this code so I put the messageBox in to test that theory.
            }
        }
     }
  }
}

I am starting and stopping notepad for now just for testing.

can anyone see what I am doing wrong?

First: what is causing notepad to stop and then restart again even though the other application is still running?

Second: if there is anything else I should be doing differently please point that out as well.

As always, thank you so much for any help you can provide.

EDIT: I didn't mention it before because of my lack of knowledge I didn't think it would be relevant.

The entire scope of the project is that I am writing an integration between two existing applications. If the one application is not running then there is no need for my application to use resources. So my thought was that a watchdog would take up fewer resources than the application itself.

My integration application uses multiple threads and gets and sets a lot of information between the other two applications.

The end user will start and stop application A whenever needed.

Application B is my integration application.

Application C - the one previously not mentioned - runs as a service and interacts with a database.

The watchdog application in question is simply to start and stop my integration application whenever Application A stops or starts.

NewToC
  • 1
  • 4
  • so what happens if someone or something is causes Application `B` to stop.. also I would ask.. `Have you stepped through the code using the debugger` you probably are doing something incorrect with the starting and stopping of a `Timer` also `goto` statements in C# are a nightmare we actually `Fire Developers` on our staff whom use this terrible coding style.. – MethodMan Nov 27 '15 at 22:56
  • if something or someone stops application B then my application will see that application B is not running and restart it. I'm not using any goto statements. I simply call CheckForApplication.Checking() from program.cs. the rest is done in what I have posted. – NewToC Nov 28 '15 at 00:04

1 Answers1

2

There is simply no need for such a 'watchdog'. Use Job objects and bind the processes in a job. Read Destroying all child processes (and grandchildren) when the parent exits. See Working example of CreateJobObject/SetInformationJobObject pinvoke in .net? for C# examples.

For process start, use WMI Win32_ProcessStartTrace, see .NET Process Monitor.

Community
  • 1
  • 1
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Please correct me if I'm wrong but it seems after reading the links that you posted that what you're suggesting is creating a job (which is actually another program) that will watch the parent process (Application A) and either start or stop my application based on if Application A is running or not. Is that the basic idea? if so how does this differ from just using my watchdog program? It seems to me that the job object would use more resources than my watchdog. Again, this is all new to me so I could be completely wrong on this. Please explain. – NewToC Nov 28 '15 at 05:10
  • A job is not another program. It is a kernel object and processes can join jobs. Jobs can impose restrictions on the member processes, including force shutdown when the job terminates. Inheriting the job on child process creation is *one* way to join a job. But you can also add existing, running processes, provided you have the required privileges. – Remus Rusanu Nov 28 '15 at 09:13