Page 1 of 1

Waiting for window existence

Posted: Mon Apr 27, 2009 2:52 pm
by IanG
I've looked through available documentation, and can't seem to find the answer I'm looking for.

How would I go about waiting on a window to appear (say a main app window post-logon) with adequate timeout in case it never appeared?

Thanks in advance.

Posted: Mon Apr 27, 2009 4:25 pm
by Ciege
Here was my solution:

Code: Select all

        private static Ranorex.Form WaitForWindow(string WindowName, int Timeout)
        {
            /************************************************************************
             * Function         : WaitForWindow(string WindowName, int Timeout)
             *
             * Description      : This function will wait for a window with a specific  
             *                  :  Title to appear.
             *                         
             * Parameters       : WindowName        - Title of the window to wait for
             *                  : Timeout           - Time (in seconds) to wait for the window
             *                                      -  before giving up.
             *                  
             * Return Type      : Ranorex.Form
             * 
             * Return Value     : Ranorex.Form Object for success, null for failure
             * 
             * Revision History
             * Date       : Author                    : Reason/Change
             * ---------- : ------------------------- : ------------------------------
             * 03/05/2009 : Chris Gendreau            : Initial Creation 
            ************************************************************************/

            Ranorex.Form HDForm = null;
            int error = -1;
            bool boolFound = false;
            int intCount = 0;

            Report.Debug("Waiting for window: " + WindowName);
            while (boolFound == false)
            {
                try
                {
                    HDForm = Host.Local.FindChild<Ranorex.Form>(WindowName);
                    try
                    {
                        HDForm.Activate();
                        error = 0;
                        boolFound = true;
                    }

                    catch (RanorexException e)
                    {
                        error = -1;
                        boolFound = false;
                    }
                }

                catch (RanorexException e)
                {
                    //Use DoEvents here so we don't get a ContextSwitchDeadlock error while debugging.
                    Application.DoEvents();
                    boolFound = false;
                    Thread.Sleep(1000);
                    intCount++;
                    if (intCount == Timeout) break;
                    error = -1;
                }
            }

            if (boolFound == true)
            {
                HDForm.Activate();
            }
            Thread.Sleep(1000);
            return HDForm;
        } //End WaitForWindow

Posted: Mon Apr 27, 2009 4:56 pm
by Support Team
I suggest using something like this:

Code: Select all


try
{
  // wait 10s for a window with title "My Main App"
  Form myForm = Host.Local.FindSingle("/form[@title='My Main App']", 10000);

}
catch(ElementNotFoundException)
{
  // your error handling code goes here
}

Michael
Ranorex Team

Posted: Mon Apr 27, 2009 5:14 pm
by Ciege
Well that’s just a little more elegant than my solution now isn't it!
:oops:

Posted: Mon Jun 01, 2009 9:34 pm
by jadoti
I'm new to Ranorex, and am still trying to get the lay of the land, so to speak.

I am working on a demo test in which I need to wait for a form to pop up. I see this code, but I'm not understanding where that's supposed to go if I'm not supposed to modify the code in the Recording1.cs file.

The Recording1.UserCode.cs only has the class initializer, but I'm guessing I can add a method with this "waiting code" in it, but how am I to call it if I'm not supposed to modify the code in the Recording1.cs file?

Thanks for your help,
Mike

Posted: Tue Jun 02, 2009 3:12 pm
by Support Team
Usually, you need this additional code only when you are not using Ranorex repositories, since the repository items themselves have a Timeout property that determines the waiting time. Just set the Timeout property of the corresponding repository items.

Regards,
Alex
Ranorex Support Team