Occasional Exception when selecting Form.Close

Ask general questions here.
User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Occasional Exception when selecting Form.Close

Post by Ciege » Thu Apr 01, 2010 7:35 pm

Very occasionally I get the following exception when I submit a Form.Close:

Code: Select all

Ranorex.ActionFailedException: Action 'close' failed on element '{Form:Foundation Setup Data Register - MASTER JOB}'. ---> System.InvalidOperationException: The control does no longer exist.
at Ranorex.Plugin.WinFormsFlavorElement.InvokeMethod(BindingFlags flags, String name, Object[] args)
at Ranorex.Plugin.WinFormsFlavorElement.InvokeAction(Element element, String name, Object[] args)
at Ranorex.Core.Element.InvokeAction(String name, Object[] args)
--- End of inner exception stack trace ---
at Ranorex.Core.Element.InvokeAction(String name, Object[] args)
at Ranorex.Form.Close()
at H_MathRegression_Alternate.Program.Test19(Form HDClientForm, Int32 intCostSummaryPrecision, String[] strArrayTest19Before, String[] strArrayTest19After) in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\H$MathRegression_Alternate\H$MathRegression_Alternate\Program.cs:line 8576

What happens is some work is done on the form then the form.close is called, the form is closed then the exception is thrown. This only happens VERY occasionally and is not readily reproducible. The same portion of code can be called hundreds of times and everything works but once in a while I will get this exception.

This current test pass was in Ranorex version 2.2.3.8084 on Windows 7 Ultimate but has been seen on other versions of Ranorex and other Windows OS's.

Any ideas as to why this may happen so infrequently?
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Occasional Exception when selecting Form.Close

Post by Support Team » Fri Apr 02, 2010 10:46 am

My guess is that sometimes the process of the closed form has already exited before Ranorex got a return message from the Form.Close operation. Usually, you see that exception when you try to invoke an action on a WinForms control that has already gone. Ranorex sends a window message to the control and if the control does not respond to that message, Ranorex thinks the control is no longer there.

When you invoke Form.Close on a form, the form is instructed to close. By default it does not immediately close and end the process, but keeps alive until the currently executed window messages is processed. So, IMHO that exception should never be thrown. However, if the form/application reacts to the Form.Closed event and in an event handler terminates the process immediately, then the message response might not get sent.

It tried to reproduce that exception with a sample application, but have not succeeded, yet (after around 1000 open/close cycles). Could it be that in your application you invoke a process termination on the Form.Closing/Closed event, e.g. by calling Environment.Exit?

Regards,
Alex
Ranorex Support Team
.
Image

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

Re: Occasional Exception when selecting Form.Close

Post by Ciege » Fri Apr 02, 2010 3:47 pm

Well I do not call Environment.Exit within my code. Could it be called in the AUT itself, unknown for sure but I assume not.
The way the AUT is architected is forms get open within the main form. Some of the forms have a dedicated OK or Cancel button but not all. The ones that do not need to be closed by either the X button or by selecting File -> Close from the forms menu bar.

The way my automation works is it opens a form within the main AUT, works on it (adds/deletes/retrieves data) then closes the form with Form.Close. Then it continues to the next step which is either opening another form or interacting some other way within the AUT. Through the testing of a particular script only a handful of Forms are opened however they are opened hundreds of times as data is entered into one then verified that the data traverses throughout the AUT correctly. When I am done on a form I close it with Form.Close.
The trouble is that I could have opened a form, manipulated it, then called Form.Close several times fine, then one time it will throw the exception. If I run the same script again it could work fine on that exact same step then possibly throw the exception from another test that is interacting with the form.
What I have done is created my own FormClose method that will call Form.Activate and Form.Valid then try/catch around Form.Close to catch the exception if it occurs. This way I can successfully close the form every time and not crash my script with this exception since the form does close correctly but only sometimes thrown the exception.

Clear as mud?
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Occasional Exception when selecting Form.Close

Post by Support Team » Tue May 04, 2010 8:21 am

I could finally reproduce that issue. An InvalidOperationException is thrown if the AUT closes before Ranorex can get the return data from the Form.Close call. As we do not need the return data for that call, I could add some special exception handling for the Form.Close action. This fix will be contained in the next maintenance release 2.3.1!

Regards,
Alex
Ranorex Support Team
.
Image

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

Re: Occasional Exception when selecting Form.Close

Post by Ciege » Tue May 04, 2010 5:09 pm

Woohoo! Awesome. 1) I am not crazy and 2) this will be a big help since this exception is painful when it happens. Everything running along fine then an "invalid" exception occurs when closing a form and basically trashes my test.

Thank you for keeping on top of this one! It will be a BIG help!
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...