Page 1 of 1

Exception when method moved from .CS file to a DLL

Posted: Tue Aug 11, 2009 6:13 pm
by Ciege
I have moved a lot of "framework" methods (methods shared between different test scripts) to a separate DLL. However, I am getting an Exception when calling a method within the DLL from one test script but not the other.

Code: Select all

System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List`1.Enumerator.MoveNextRare() at System.Collections.Generic.List`1.Enumerator.MoveNext() at Ranorex.Core.ElementEngine.ApplyGlueRules(Element element, GlueRuleExecutionState state) at Ranorex.Core.ElementEngine.ApplyGlueRules(Element element) at Ranorex.Core.Element.GetChildren(CacheSession session) at Ranorex.Core.Element.get_Children() at Ranorex.Core.RxPath.collectAxis(LocationStep step, Axis optimizedAxis, Element self, IList`1 elements, Boolean quitOnFirstGoodNode) at Ranorex.Core.RxPath.ApplyInternal(RxPath path, Element startElement, IDataProvider dataProvider, Duration timeout, Boolean findSingle) at Ranorex.Core.Element.FindSingle(RxPath path, Duration timeout) at Ranorex.Adapter.FindSingle(RxPath path, Duration timeout) at RanorexFramework.RFW.WaitForWindowPartialName(String WindowName, Int32 Timeout) in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\RanorexFramework\RanorexFramework\Class1.cs:line 949 at Estimating_Regression_Test_1.Program.HardDollarEstimatingRegressionTest1() in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\Estimating Regression Test 1\Estimating Regression Test 1\Program.cs:line 161 at Estimating_Regression_Test_1.Program.Main(String[] args) in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\Estimating Regression Test 1\Estimating Regression Test 1\Program.cs:line 106 
The line of code that is throwing the exception is:

Code: Select all

HDForm = Host.Local.FindSingle("/form[@title~'" + WindowName + ".*']", Timeout * 1000);
If I move this method out of the DLL and into the .CS it works fine.
Also, If I call this method from another test script it works fine.
I have just createad another test script and this is one of the very first calls. For whatever reason it is failing in the new test script but not the existing test script.

Any ideas?

Re: Exception when method moved from .CS file to a DLL

Posted: Tue Aug 11, 2009 6:29 pm
by Ciege
Forgot to mention, this is with Ranorex version: 2.1.0.6243 on Windows XP using Visual Studio 2008 with C#.

Thanks.

------------------------

More updates:
Now sometimes this works correctly and sometimes there is no Exception but the form is never found.

Am I running into a race condition of some sort within Ranorex?

Re: Exception when method moved from .CS file to a DLL

Posted: Wed Aug 12, 2009 10:00 am
by Support Team
With 2.1.0 there could be a race condition when several threads try to initialize the Ranorex framework, i.e. access Ranorex for the first time since the process has been started. Do you have several threads that access Ranorex in your application?

Please, also try reproducing that issue with the latest Ranorex version (2.1.3). Thanks!

Regards,
Alex
Ranorex Support Team

Re: Exception when method moved from .CS file to a DLL

Posted: Wed Aug 12, 2009 4:02 pm
by Ciege
Hi,

I have two threads. The main testing thread then a second thread just checking for AUT exceptions (as described in this thread: http://www.ranorex.com/forum/second-ran ... -t916.html).

The main thread is marked with STAThread and the second thread is started with an apartment state of STA workerThread.SetApartmentState(ApartmentState.STA);.

The DLL also has the STAThread marker at the top of the class, although I don't know if that makes a difference.

Again, what is interesting is I cannot reproduce this exception if I move the DLL code back into my main .CS file and call the method locally as opposed to calling it from the DLL.

I will try upgrading to the current Ranorex release and let you know what I find out.

Re: Exception when method moved from .CS file to a DLL

Posted: Wed Aug 12, 2009 5:03 pm
by Support Team
With Ranorex V2.1.0 you can also avoid that race condition if you execute the following code in your main thread before starting any other threads:
Host.Local.FindChildren<Form>();
That should do all the initialization stuff in Ranorex. From V2.1.1 on the initialization of the Ranorex framework should be thread-safe.

Regards,
Alex
Ranorex Support Team

Re: Exception when method moved from .CS file to a DLL

Posted: Wed Aug 12, 2009 6:22 pm
by Ciege
I did not upgrade yet, but that line of code did help my issue. It all seems to be working again now.

Thanks!

Re: Exception when method moved from .CS file to a DLL

Posted: Thu Aug 13, 2009 8:08 am
by Support Team
Good, than it was the issue with the framework initialization that was already fixed in V2.1.1 :-)

Regards,
Alex
Ranorex Support Team