title: Wait for UI to load new window before continuing
body: <p>I am novice of VB.Net trying to create a plugin for a program with a fairly lacking API.  Essentially what I am trying to accomplish is press a button in the interface, which will create a new window.  Once this window loads, I have a couple other buttons to press to eventually print out an HTML file it generates.</p>
  
  <p>I attempted to use Windows UI Automation, but was unable to manipulate the first control (it appeared as a "pane" element for some reason, and had no supported control patterns)</p>
  
  <p>I eventually was able to use <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx" rel="nofollow">PostMessage</a> to send a MouseDown and MouseUp message to the control to activate it.</p>
  
  <p><strong>My question is</strong>:  What is the best way for me to wait for this window to finish loading before continuing my code execution?</p>
  
  <p>I tried using Thread.Sleep after I sent the click to my control, but my code seemed to trigger the sleep before it even started to load the window.  I suspect there may be some kind of event-driven options, but I don't even know where I would begin with that.</p>
  
  <p>Side note: I do have access to the program's Process ID if that helps.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>To be more explicit about what I have done, here is some code for you to look at:</p>
  
  <pre><code>' These variables declared further up in my program, when plugin is loaded
  Private aeDesktop As AutomationElement
  Private aeRadan As AutomationElement
  Private aeBlocksBtn As AutomationElement
  
  Private Sub UITest1()
  Dim rpid As Integer
  Dim intret As Integer
  
  ' Note: mApp is declared further up in the code, when the plug in initializes
  ' It sets a reference to the application object (API command from the software)
  
  rpid = mApp.ProcessID
  
  aeDesktop = AutomationElement.RootElement
  Dim propcon As New PropertyCondition(AutomationElement.ProcessIdProperty, rpid)
  Dim propcon2 As New PropertyCondition(AutomationElement.NameProperty, "big_button_blocks.bmp")
  
  aeRadan = aeDesktop.FindFirst(TreeScope.Children, propcon)
  aeBlocksBtn = aeRadan.FindFirst(TreeScope.Descendants, propcon2)
  
  ' MAKELONG is a function that concatenates given x and y coordinates into an appropriate "lparam" value for PostMessage.
  ' Coordinates 20, 20 are chosen arbitrarily
  Dim lParam As Long = MAKELONG(20, 20)
  
  intret = PostMessage(CInt(aeBlocksBtn.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty)), &amp;H201, &amp;H1, lParam)
  intret = PostMessage(CInt(aeBlocksBtn.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty)), &amp;H202, &amp;H0, lParam)
  
  
  Dim bwinprop As New PropertyCondition(AutomationElement.NameProperty, "Radan Block Editor")
  Dim aeblockwin As AutomationElement
  Dim numwaits As Integer = 0
  
  Do
  numwaits += 1
  Thread.Sleep(100)
  aeblockwin = aeRadan.FindFirst(TreeScope.Children, bwinprop)
  Loop While (numwaits &lt; 50) AndAlso (aeblockwin Is Nothing)
  
  If numwaits &gt;= 50 Then
  MessageBox.Show("ERROR: Block Editor Window Not found")
  End If
  </code></pre>
  
  <p>I am fairly sure what is happening has to do with the code moving to the Do While loop before the PostMessage is processed by the program.  I have tried using SendMessage to try to bypass this, but unfortunately that does not seem to work.</p>
  
  <p>I feel like there is a pretty simple solution here, like maybe some kind of alternate wait or sleep command that I don't know about, so maybe someone could help guide me to it?</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Screenshot of the <a href="http://postimg.org/image/618o0uqwt/full/" rel="nofollow">inspect.exe output</a> for this control.</p>
  
  <p>Also, a screenshot of the <a href="http://postimg.org/image/a8w8midaz/full/" rel="nofollow">user interface</a>.  This is from Radan, a CAM software I use for nesting and processing sheet metal parts to be laser cut.  I put a red box around the control I would like to activate.</p>