simonso
simonso

Reputation: 595

Windows 10: How to determine whether a process is an App/Background Process/Windows Process

Programmatically, how can I determine the 3 categories in Windows 10

Just like Task Manager?

i.e. I need some C# code which I can determine a list of Apps vs. a list of background process. Checking executionState in Win32_process doesn't work. It's always null.

Thanks!

enter image description here

Upvotes: 2

Views: 4211

Answers (1)

simonso
simonso

Reputation: 595

The original problem:

  • How to detect list of running apps?

I have a different take regarding the solution:

Some UWP apps has the main window title. Checking main window title is not enough to say the app is running or not.

UWP apps in suspend state will still return the title (see the red rectangle)

enter image description here

So in order to detect the suspend state, we need to cover

  1. app has title but not running in the foreground
  2. app has title but not running in the background

{code}

static void byProcess()
    {
        Process[] processes = Process.GetProcesses();
        List<string> suspendedAppsWhichHasTitle = new List<string>();

        foreach (var proc in processes)
        {
            // if it has main window title
            if (!string.IsNullOrEmpty(proc.MainWindowTitle))
            {
                // the app may be in suspend state 
                foreach (ProcessThread pT in proc.Threads)
                {
                    ThreadState ts = pT.ThreadState;
                    if (ts == ThreadState.Running)
                    {
                        suspendedAppsWhichHasTitle.Add(proc.MainWindowTitle);
                    }
                }                    
            }
        }

        foreach(string app in suspendedAppsWhichHasTitle)
        {
            Console.WriteLine(app);
        }

        if (suspendedAppsWhichHasTitle.Count == 0)
        {
            Console.WriteLine("No visible app is running!");
        }
        Console.Read();
    }

}

Upvotes: 1

Related Questions