سؤال

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!

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

It's probably due to that (from the documentation):

SendMessage calls the window procedure for the specified window and does not return until the window procedure has processed the message.

I 'm not sure what exactly the target window does as a response to your message, but if it stops pumping messages (e.g. to wait for user input) then your own program will also stop and wait.

An alternative to this is PostMessage, which always returns without waiting.

Is this a typo? The window receiving the button message is not the same as the one found. Normally, a window's message proc is structured so that unhandled messages are passed to the default windows proc and, in general, are engineered to be quick. However, if the window proc, for whatever reason, fails to return then the application is hung. Could it be that childHandle's message proc is in an endless loop? There is a special window handle, 0xffff, which is an indication that the message should be sent to all top level windows of all applications currently running. Alternatively, use PostMessage instead of SendMessage. PostMessage will always return immediately.

buttonHandle = FindWindowEx(parentHandle, new IntPtr(0), new IntPtr(0), windowTitle); SendMessage(childHandle, BM_CLICK, new IntPtr(0), "");

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top