Hi,
I believe i've solved it for those out there looking for an answer:
I am not sure if there is an easier way, if there is, please suggest.
Added a class to my application:
class ActiveProcess
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private static Process GetProcessByHandle(IntPtr hwnd)
{
try
{
uint processID;
GetWindowThreadProcessId(hwnd, out processID);
return Process.GetProcessById((int)processID);
}
catch { return null; }
}
private static Process GetActiveProcess()
{
IntPtr hwnd = GetForegroundWindow();
return hwnd != null ? GetProcessByHandle(hwnd) : null;
}
public bool IsRunning(string File)
{
bool Loadcative = false;
try
{
Process currentProcess = GetActiveProcess();
var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", currentProcess.Id);
var search = new ManagementObjectSearcher("root\\CIMV2", query);
var results = search.Get().GetEnumerator();
var queryObj = results.Current;
uint parentId = (uint)queryObj["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);
if ((parent.ProcessName == File))
Loadcative = false;
else
Loadcative = true;
}
catch { }
return Loadcative;
}
}
and then calling it like this on a timer:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void Tmr_CheckMap_Tick(object sender, EventArgs e)
{
string filetocheck = "enterparentfilenamehere";
Process currentProcess = Process.GetCurrentProcess();
Process[] pname1 = Process.GetProcessesByName(filetocheck);
ActiveProcess AP = new ActiveProcess();
if (pname1.Length != 0 && AP.IsRunning(filetocheck))
{
SetForegroundWindow(pname1[0].MainWindowHandle);
}
}