I\'m trying to find a way to get the open tasks in C#. I\'ve been searching on google and can only find how to get a list of the processes. I want the onl
I haven't tried it, but I suspect something like this:
using System.Diagnostics;
static void MyFunc()
{
Process[] processes = Process.GetProcesses();
foreach(Process p in processes)
{
if (p.MainWindowHandle != 0)
{ // This is a GUI process
}
else
{ // this is a non-GUI / invisible process
}
}
}
The point is to check each process for a WindowHandle.
From an API (Win32) perspective there is no such thing as Tasks (at least not the one that Windows Task Manager/Alt-Tab shows).
Those "Tasks" are actually top level windows.
So in order to get a list of those, you need to enumerate the windows (here is the PInvoke for it).
Then look at the style of the windows to determine if they are actually top level windows.
This article should pretty much tell you exactly what to do, it shows how to build your own task switch and includes the code needed to enumerate all windows and determine if they are "tasks" and it shows you how to use PrintWindow api to get the previews on XP.
http://msdn.microsoft.com/en-us/library/ms997649.aspx
Also, here is a blog post that talks about the algorithm used to determine what shows up in the Alt+Tab view. Basically you need to check the WS_EX_APPWINDOW and WS_EX_TOOLWINDOW along with if the window has an owner.
@abelenky17
I suspect that this will not cover all cases, for example there are processes who have several top level windows (that all appear in the task manager). Consider for example: FireFox, Windows Explorer, IE, etc... those applications can have multiple windows on the desktop. Also, it will not handle Terminal Sessions scenario's properly (because you enumerate all the processes running in the system).
@Dan C.
Doing something like this:
p.ProcessName != "explorer"
seems ok to you? It smells, bad.