Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below is code to find the Active process whose Window state is Maximized && Normal. But is takes more CUP. Please help me to reduce the CPU usage. It is taking 18% of the CUP, while running diagnosis.

What I have tried:

C#
IEnumerable<Process> processes = Process.GetProcesses()
               .Where(p => (long)p.MainWindowHandle != 0
                         && p.MainWindowTitle.Length > 0
                         && (p.StartInfo.WindowStyle == ProcessWindowStyle.Maximized
                                 || p.StartInfo.WindowStyle == ProcessWindowStyle.Normal)
                         && (WindowState.GetWindowState(p.MainWindowHandle) == "Maximized"
                                 || WindowState.GetWindowState(p.MainWindowHandle) == "Normal")
                         );



C#
public static class WindowState
    {
        private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
        {
            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
            placement.length = Marshal.SizeOf(placement);
            GetWindowPlacement(hwnd, ref placement);
            return placement;
        }

        public static string GetWindowState(IntPtr hwnd)
        {
            WINDOWPLACEMENT placement = GetPlacement(hwnd);

            if (placement.showCmd == ShowWindowCommands.Minimized)
                return "Minimized";

            if (placement.showCmd == ShowWindowCommands.Maximized)
                return "Maximized";

            if (placement.showCmd == ShowWindowCommands.Hide)
                return "Hide";

            if (placement.showCmd == ShowWindowCommands.Normal)
                return "Normal";

            return string.Empty;
        }

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetWindowPlacement(
            IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
        internal enum ShowWindowCommands : int
        {
            Hide = 0,
            Normal = 1,
            Minimized = 2,
            Maximized = 3,
        }

        [Serializable]
        [StructLayout(LayoutKind.Sequential)]
        internal struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public ShowWindowCommands showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    }
Posted
Comments
Richard Deeming 10-Mar-20 7:34am    
The only thing that jumps out at me is that you're calling GetWindowState twice for each process.

It's also odd that you're converting an integer to a string, when string comparison is less efficient than integer comparison.

Beyond that, you'll need to profile your code to find out where the bottleneck is. If you don't already have a profiler, try CodeTrack[^].
Babu.R.K 11-Mar-20 9:19am    
Thanks, it really helped.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900