Example #1
0
        //Posts a thread message to the first enumerated thread (when ensureTargetThreadHasWindow is false), or posts a thread message to the first enumerated thread with a window, unless no windows are found in which case the call fails.  If an appropriate thread was found, the return value is the return value of PostThreadMessage call, otherwise the return value is false.
        public static bool PostThreadMessage(this Process p, uint msg, IntPtr wParam, IntPtr lParam, bool ensureTargetThreadHasWindow = true)
        {
            uint targetThreadId = 0;

            if (ensureTargetThreadHasWindow)
            {
                IntPtr hwnd      = p.WindowHandles().FirstOrDefault();
                uint   processId = 0;
                if (hwnd != IntPtr.Zero)
                {
                    targetThreadId = Win32Helpers.GetWindowThreadProcessId(hwnd, out processId);
                }
            }
            else
            {
                targetThreadId = (uint)p.Threads[0].Id;
            }
            if (targetThreadId != 0)
            {
                return(Win32Helpers.PostThreadMessage(targetThreadId, msg, wParam, lParam));
            }
            else
            {
                return(false);
            }
        }