2

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

  • Apps
  • Background process
  • Windows service

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

simonso
  • 595
  • 1
  • 8
  • 19
  • Getting background processes is relatively easy, but the rest is a lot of work. That's really 3 questions in one, and you haven't shown any effort to solve any of the problems. – Barmak Shemirani Feb 25 '16 at 08:28
  • 1
    I tried to use if (null != System.Diagnostics.Process.MainWindowTitle) to determine the app is in background or not. However, it is not it was meant to be, e.g. Windows.Mobile.Store (Win10) will be in suspend state - background process even if I close the app, and MainWindowTitle will not be null. Hence not the result I wanted. Also tried Win32_Process. None of the properties indicated that the process is background or not. I only need to distinguish Apps vs. Background process. Care the less about Windows Service. Thanks for your input and observation. – simonso Feb 26 '16 at 17:50
  • In WinAPI, to find the apps, you would use `EnumWindows`, then check to see if window is visible, check if window is "alt-tab" window, make sure window is not hidden Windows 10 app. – Barmak Shemirani Feb 26 '16 at 19:20

1 Answers1

1

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();
    }

}

simonso
  • 595
  • 1
  • 8
  • 19