Where to put STAThread if Ranorex is used in separate thread

Class library usage, coding and language questions.
AKalytiuk
Posts: 9
Joined: Fri Mar 04, 2011 2:36 pm
Location: Kyiv, Ukraine

Where to put STAThread if Ranorex is used in separate thread

Post by AKalytiuk » Mon Aug 05, 2013 2:15 pm

Hi,

There are 2 questions in my topic:
  • 1. Where exactly Appartment State should be set to STA (STAThread applied) for situations where all calls to Ranorex API are made in separate thread?
    2. Where can detailed documentation about Appartment State STA that is required for Ranorex API calls be found?
For the sake of test timeout functionality we execute all the code related to test cases (which includes calls to Ranorex API) in separate thread.

So 1st question is: which threads' Appartment State should be set to STA? Main thread of the application that runs test code OR namely the thread where calls to Ranorex API are executed?

2nd question: Please provide a link to help article that covers all aspects related to STA Apartment State that is required for Ranorex usage? (I have already found many topics that reference STAThread attribute in short - including this user guide article http://www.ranorex.com/support/user-gui ... ation.html - is there any more in detail documentation about relations between Ranorex libraries functionality and parameters of a thread that makes calls to that library?)

I have already noticed that Ranorex functionality works OK only when [STAThread] attribute is set on Main method only - for the application that makes calls to Ranorex API. For situations when Appartment State is set to STA for the thread where calls to Ranorex API are executed (as in mock-up code below) - then location of controls does not work. Could you please explain the reasons of such behavior?

Code: Select all

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread automationThread = new Thread(executeAutomation);

            automationThread.Start();

            // Makes locations of controls not execute successfully
            automationThread.SetApartmentState(ApartmentState.STA);
        }

        private static void executeAutomation
        {
            // Make calls to Ranorex API
        }
    }
}
Edti

Problem was that Apartment state STA was set for Ranorex thread after thread start (unless some magic interfered me). After setting Apartment State STA before Ranorex thread start location of controls by Ranorex API in separate thread works OK.

Thanks very much for your reply.
Last edited by AKalytiuk on Mon Aug 12, 2013 7:31 am, edited 1 time in total.

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

Re: Where to put STAThread if Ranorex is used in separate thread

Post by Support Team » Tue Aug 06, 2013 3:37 pm

Every thread which executes and access Ranorex code must be set to STA (Single Threaded Apartment) COM model. Otherwise technologies relying on COM, e.g. MSAA, WPF, or WinForms, would not work or deliver incorrect results.

Usually, you set the apartment state for a thread using the Thread.SetApartmentState before you start the thread.
Only for the Main thread of an application you cannot do so, because the thread is already created when the code would run. That's why you need to use the STAThread attribute (see the linked MSDN documentation for more information).
AKalytiuk wrote:For situations when Appartment State is set to STA for the thread where calls to Ranorex API are executed (as in mock-up code below) - then location of controls does not work. Could you please explain the reasons of such behavior?
I cannot reproduce that problem. As I wrote above, usually only those threads interacting with the Ranorex API need to have the STA state. Could you explain the issue in more detail, possible posting a Ranorex snapshot or sample?

Regards,
Alex
Ranorex Team
.
Image