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.