locked
STA Thread error when running unit tests in a Visual Studio 2005 load test

    Question

  • I'm getting errors when trying to run a unit test as part of a load test. It doesnt actually get going as the errors occur straight away and quickly reaches 1000 causing the test to abort. It appears to be a threading issue although we have put in something to deal with the thread state based on the error text the load test run provides:


    <none> TestError TestError Test method LoadTest.Search.Searchtest threw exception:  System.Threading.ThreadStateException: The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.. view -

     

    and the accompanying stack trace:

     

     

        at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, LogonDialogHandler logonDialogHandler, Boolean createInNewProcess)
       at WatiN.Core.IE..ctor(String url)
       at LoadTest.Search.Searchfortracks() in (project file address)

     

    As you may well see from the stack trace, the unit tests were originally recorded using the WatiN recorder and imported as C# scripts. The test runs perfectly from test manager as a stand alone test.

    Thursday, January 17, 2008 4:57 PM

Answers

  • Unfortuntely, it's not possible to change ApartmentState when running under LoadTest. LoadTest emulates virtual users by creating threads, each thread is created with ApartmentState.MTA and there is no way to change that, actually, neither in VS2005 nor in VS2008. Then inner test of a Load Test is run in that thread.

     

    When runnig Unit Test separately, the thread is controlled by test execution framework, be default it is STA, and you can change that by specifying in TestRunConfig (the example below is for VS 2005):

        <apartmentState type="System.Threading.ApartmentState">
          <value__ type="System.Int32">1</value__>
        </apartmentState>

    If you have to run inside STA for Unit Tests under Load Test, I don't see other way than to create the threads manually, like this:

                Thread t = new Thread(new ThreadStart(delegate()
                {

                     // Here comes your Unit Test body. 

                }));

                t.ApartmentState = ApartmentState.STA;

                t.Start();
                t.Join();

    Thanks,

    Michael

    Thursday, January 24, 2008 7:00 AM

All replies

  • Alistair, which version of Visual Studio are you using?

    Thanks,

    Michael

    Friday, January 18, 2008 8:59 AM
  •  

    Hi Michael, sorry I forgot to include that, its VS2005, I've added this into the subject header as well now.
    Friday, January 18, 2008 9:54 AM
  • Unfortuntely, it's not possible to change ApartmentState when running under LoadTest. LoadTest emulates virtual users by creating threads, each thread is created with ApartmentState.MTA and there is no way to change that, actually, neither in VS2005 nor in VS2008. Then inner test of a Load Test is run in that thread.

     

    When runnig Unit Test separately, the thread is controlled by test execution framework, be default it is STA, and you can change that by specifying in TestRunConfig (the example below is for VS 2005):

        <apartmentState type="System.Threading.ApartmentState">
          <value__ type="System.Int32">1</value__>
        </apartmentState>

    If you have to run inside STA for Unit Tests under Load Test, I don't see other way than to create the threads manually, like this:

                Thread t = new Thread(new ThreadStart(delegate()
                {

                     // Here comes your Unit Test body. 

                }));

                t.ApartmentState = ApartmentState.STA;

                t.Start();
                t.Join();

    Thanks,

    Michael

    Thursday, January 24, 2008 7:00 AM
  • Thanks for this, I'll get one of the devs to have a look at implementing this.

     

    Are you saying though that all load tests want to run tests as MTA but all unit tests are STA by default? This doesnt happen for the web tests I've created, just the unit tests so is this a limitation of running unit tests for load purposes?

    Monday, January 28, 2008 3:26 PM
  • FYI, we are fixing this in VS 2008 SP1.

     

    Ed.

    Sunday, February 10, 2008 12:33 AM
    Moderator
  • Excellent, thanks.

    Monday, February 11, 2008 2:14 PM
  •  Michael Koltachev - MSFT wrote:

    Unfortuntely, it's not possible to change ApartmentState when running under LoadTest. LoadTest emulates virtual users by creating threads, each thread is created with ApartmentState.MTA and there is no way to change that, actually, neither in VS2005 nor in VS2008. Then inner test of a Load Test is run in that thread.

     

    When runnig Unit Test separately, the thread is controlled by test execution framework, be default it is STA, and you can change that by specifying in TestRunConfig (the example below is for VS 2005):

        <apartmentState type="System.Threading.ApartmentState">
          <value__ type="System.Int32">1</value__>
        </apartmentState>

    If you have to run inside STA for Unit Tests under Load Test, I don't see other way than to create the threads manually, like this:

                Thread t = new Thread(new ThreadStart(delegate()
                {

                     // Here comes your Unit Test body. 

                }));

                t.ApartmentState = ApartmentState.STA;

                t.Start();
                t.Join();

    Thanks,

    Michael



    ...and for a VS 2008 example, in the TestRunConfiguration element, add:

        <ExecutionThread apartmentState="1" />

    Where 1 = System.Threading.ApartmentState.MTA

    Wednesday, May 07, 2008 9:08 PM
  • That's right. In VS2008 we changed XML schema to be more user-friendly and it becomes 
    <ExecutionThread apartmentState="1" />
    or
    <ExecutionThread apartmentState="MTA" />
    Note that you can should be able to use MTA/STA (values of the enum as strings) instead of 0/1.

    Thanks,
    Michael
    Wednesday, November 19, 2008 5:32 PM
  • Is there any way to modify this other than testrunconfig, IE at mstest runtime or in test code in initialize methods maybe?
    -Dano
    Friday, November 21, 2008 1:51 AM
  • You may have to reload your solution for it to pick up the <ExecutionThread apartmentState="MTA" /> change in the .testrunconfig, VS2008 sometimes doesn't see the change.
    Tuesday, January 20, 2009 9:40 PM
  • For those like me who look for this thing in VS 2010: http://msdn.microsoft.com/en-us/library/ms404663%28v=VS.100%29.aspx

    Hope it saves your time


    Reza
    Thursday, November 11, 2010 12:29 AM