1

We have a legacy application with ThreadPool and WaitHandle. We wanted to write Unit-Tests using MSTest against it. We ran into a problem where by default MSTest runs in STA (Single Thread Apartment) mode and the test was throwing the following exception,

System.NotSupportedException: WaitAll for multiple handles on a STA thread is not supported. at System.Threading.WaitHandle.WaitMultiple(WaitHandle[] waitHandles, Int32 millisecondsTimeout, Boolean exitContext, Boolean WaitAll) at System.Threading.WaitHandle.WaitAll(WaitHandle[] waitHandles, Int32 millisecondsTimeout, Boolean exitContext) at System.Threading.WaitHandle.WaitAll(WaitHandle[] waitHandles, TimeSpan timeout, Boolean exitContext)

Raajkumar
  • 857
  • 2
  • 13
  • 26

1 Answers1

2

After researching for a while, we found how to change the test settings from STA to MTA.

  1. Add a test.runsettings file to you solution's folder

    <?xml version="1.0" encoding="utf-8"?>
    <RunSettings>
      <RunConfiguration>
          <ExecutionThreadApartmentState>MTA</ExecutionThreadApartmentState>
      </RunConfiguration>
    </RunSettings>
    
    
  2. Edit **.Test.csproj file and refer the test.runsettings file,

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
      ...  
      <RunSettingsFilePath>$(SolutionDir)\test.runsettings</RunSettingsFilePath>
      ...
  </PropertyGroup>
</Project> 

Now run your test, it should work.

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
Raajkumar
  • 857
  • 2
  • 13
  • 26