Click here to Skip to main content
14,209,923 members
Rate this:
Please Sign up or sign in to vote.
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 5 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 3

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".
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

// 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");
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 ... ?
   
v2

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



Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190617.3 | Last Updated 12 Jun 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100