Search code examples
c#pinvokesendmessage

C# Code hangs after Pinvoke SendMessage


I am automating test procedures for a separate Windows Form application. I am currently using pinvoke to communicate with the other application. In this application, there is a button which creates a new pop up window when clicked.

I am communicating with the buttons and fields successfully. However, when clicking the button the code following SendMessage() does not run. The button is clicked successfully and the window appears, but the following code is not ran until I manually close the window.

Here is my code:

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

.

    buttonHandle = FindWindowEx(parentHandle, new IntPtr(0), new IntPtr(0), windowTitle);
    SendMessage(childHandle, BM_CLICK, new IntPtr(0), "");
    MessageBox.Show("This won't show until I close the window");

Everything works fine, except whatever line of code comes after SendMessage() isn't ran until I close the new window created when the button is clicked.

Can anyone help explain what may be going on here?

Thanks!


Solution

  • SendMessage will block until the recipient of the call has completed processing the message.

    You could instead invoke PostMessage which will allow your program to continue executing immediately after dispatching the message.