Waiting for window existence

Ask general questions here.
IanG
Posts: 1
Joined: Mon Apr 27, 2009 2:49 pm

Waiting for window existence

Post by IanG » Mon Apr 27, 2009 2:52 pm

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.

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Post by Ciege » Mon Apr 27, 2009 4:25 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Post by Support Team » Mon Apr 27, 2009 4:56 pm

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

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Post by Ciege » Mon Apr 27, 2009 5:14 pm

Well that’s just a little more elegant than my solution now isn't it!
:oops:

jadoti
Posts: 1
Joined: Mon Jun 01, 2009 9:10 pm
Contact:

Post by jadoti » Mon Jun 01, 2009 9:34 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Post by Support Team » Tue Jun 02, 2009 3:12 pm

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