Home > OS >  Why is my program waiting for the popup to close?
Why is my program waiting for the popup to close?

Time:01-10

i am trying to work into the automation of another programs using mthe methods of user32.dll in my C# project. I created a small test program to automatically enter text inputs and click a button. Once the button is clicked, a popup appears that can be closed with the 'yes' button.

However, as soon as the popup opens, my program is waiting for it to close until it continues running its code. (I have to click the button manually)

How do i keep the code running to access the popup window?

Console.WriteLine("Please press enter to continue.");

Console.ReadLine();
identifyProcess();
RemoteMethods.updateWindows(_MainWnd);
IntPtr saveButton = RemoteMethods.GetChildWindow(RemoteMethods.GetIndexByTitle(_MainWnd, "Save")[0]);
IntPtr titleTF = RemoteMethods.GetChildWindow(3);
IntPtr nameTF = RemoteMethods.GetChildWindow(5);
IntPtr streetTF = RemoteMethods.GetChildWindow(1);

//Replacing Text

RemoteMethods.SendMessage(titleTF, RemoteMethods.WM_SETTEXT, IntPtr.Zero, "Mr.");
RemoteMethods.SendMessage(nameTF, RemoteMethods.WM_SETTEXT, IntPtr.Zero, "Berg");
RemoteMethods.SendMessage(streetTF, RemoteMethods.WM_SETTEXT, IntPtr.Zero, "Fountainally 3.");

RemoteMethods.ClickButton(saveButton);

//Popup appears here
//But the program only continues after i click 'Yes' manually.

Console.WriteLine("Test");
IntPtr nW = RemoteMethods.GetLastActivePopup(_MainWnd);
RemoteMethods.updateWindows(nW);
IntPtr yesButton = RemoteMethods.GetChildWindow(RemoteMethods.GetIndexByTitle(nW, "Yes")[0]);

RemoteMethods.ClickButton(yesButton);

Here is the RemoteMethods class: https://drive.google.com/file/d/19xtvq6ep8ICKY4WZqY0BBv8schnhzfqs/view?usp=sharing

This is what happens.

CodePudding user response:

SendMessage() will wait until the message was processed, i.e. the method handler of the save button has run to its end. This will not be the case if that handler opens another window and waits for user input (as in your case).

Use SendNotifyMessage() or Postmessage() instead. They are similar, but SendNotifyMessage() will give the message a higher priority than PostMessage(). Both methods work in an asynchronous way, i.e. they will return immediately.

This in turn means, that you might need to retry querying the next window, since it might not be shown yet directly when the method returned.

  •  Tags:  
  • Related