/// <summary>
 /// Adds a <seealso cref="ProcessFriendlyName"/> for the current process.
 /// </summary>
 public void Add(ProcessFriendlyName name)
 {
     if (name == null)
         return;
     int foundIndex = mFriendlyNames.BinarySearch(name, name);
     if (foundIndex < 0)
     {
         mFriendlyNames.Insert(~foundIndex, name);
     }
     else
     {
         mFriendlyNames.Insert(foundIndex, name);
     }
 }
Exemple #2
0
            public bool EnumDesktopWindowsDelegate(IntPtr hwnd, IntPtr lParam)
            {
                uint processId = 0;
                uint threadId = Interop.GetWindowThreadProcessId(hwnd, out processId);
                bool visible;
                DateTime windowThreadDate = DateTime.MinValue;
                string windowTitle;

                if (threadId == 0 || processId == 0)    // We somehow mystically failed to get one of the thread or process ids
                {
                    return true;
                }

                // Get window information
                var maybeWindowInfo = Interop.GetWindowInfo(hwnd);

                if (!maybeWindowInfo.HasValue)
                {
                    return true;
                }

                var windowInfo = maybeWindowInfo.Value;

                // There are some classes of windows that I don't want to display so skip them
                if (windowInfo.dwExStyle.HasFlag(WindowExtendedStyle.ToolWindow)
                    || 0 != (windowInfo.dwStyle & (WindowStyle.Popup | WindowStyle.Child | WindowStyle.Disabled)))
                {
                    return true;
                }

                // Get the text of the window
                windowTitle = Interop.GetWindowText(hwnd);
                if (string.IsNullOrEmpty(windowTitle))
                {
                    return true;
                }

                // Flag whether the window is visible.  There are some that I still want to display unfortunately
                // which means I'll also have a bunch of others that I don't want to display in the list
                visible = (windowInfo.dwStyle & (WindowStyle.Minimize | WindowStyle.Visible)) != 0;

                DateTime threadExitedTime, threadKernelTime, threadUserTime;
                // I use the thread information to determine when the window is created.  Typically the first window
                // created is really the interesting one so I sort them based on date created
                using (var threadHandle = Interop.OpenThreadHandle(ThreadAccess.QueryInformation, false, threadId))
                {
                    if (threadHandle != null)
                    {
                        Interop.GetThreadTimes(threadHandle.Value, out windowThreadDate, out threadExitedTime, out threadKernelTime, out threadUserTime);
                    }
                }

                ProcessFriendlyName windowFriendlyName = new ProcessFriendlyName(windowTitle, windowThreadDate, visible);
                ProcessInformation processInfo = null;
                if (!mProcesses.TryGetValue((int)processId, out processInfo))
                {
                    processInfo = new ProcessInformation((int)processId, string.Empty);
                    mProcesses[(int)processId] = processInfo;
                }
                processInfo.Add(windowFriendlyName);

                return true;
            }