Click here to Skip to main content
15,860,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check if main process of chrome was closed or running because when user close the browser background processes still opened, any suggestions?

What I have tried:

Process[] pname = Process.GetProcessesByName("chrome");
              
                    
              
                if (pname.Length == 0)
                {
                    Console.WriteLine("nothing");

                }
                else
                {
                    
                    Console.WriteLine("run");

                }
Posted
Updated 12-Jun-19 11:13am

You can get the "root" Chrome process using WMI. The Win32_Process[^] class has a property called ParentProcessId.

In the case where Chrome is launched by the user through launching a Chrome shortcut from Explorer, get the ProcessId of "explorer" and then get the Win32_Process instances for "chrome". The root process is the one that has a ParentProcessId matching the ProcessId of "explorer".

If Chrome is being launched by another application, like clicking a link in Outlook, or from some other application you've written, the ProcessId of the parent is going to be the one that launched Chrome, not the one for "explorer".
 
Share this answer
 
C#
// it's goofy, but do this instead
/* it will not return the "main process" though - there's no ready way to know that with chrome. Chrome will spawn multiple chrome processes even when it's "not running."
*/ 

var procs = Process.GetProcesses();
var found = false;
foreach (var proc in procs)
{
	if("chrome"==proc.ProcessName)
	{
		found = true;
		break;
	}
}
Console.WriteLine(found ? "run" : "nothing");
 
Share this answer
 
v3
This might be helpful:
// required
using System.Diagnostics;

// in some method or eventhandler

foreach (Process process in Process.GetProcesses())
{
    string pname = process.ProcessName;

    string plower = pname.ToLower();

    string title = process.MainWindowTitle;

    if (plower.Contains("chrome") || plower.Contains("google"))
    {
        Console.WriteLine($"{pname} {title}");
    }
}
When run, the current active Chrome web-page will be the only Chrome process with a 'MainWindowTitle that is not an empty string.

Of course, a version change of Chrome, could change this.

If you said more about what your goal is ... ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900