C# - Get list of open tasks

。_饼干妹妹 提交于 2019-11-28 23:42:37

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.

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.

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.

Asher

@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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!