Building in PowerShell using STA (instead of default MTA)

Mike Shepard's Avatar

Mike Shepard

26 Jul, 2018 02:07 AM

I'm writing a WPF framework in PowerShell, so the unit tests involve creating WPF objects.

when I try, I get this error:
 Describing Window functionality
    [-] Returns an object for a textbox 284ms
      InvalidOperationException: The calling thread must be STA, because many UI components require this.
      MethodInvocationException: Exception calling ".ctor" with "0" argument(s): "The calling thread must be STA, because many UI components require this."
      at New-WPFControl, C:\projects\wpfbot3000\WPFBot3000\Private\New-WPFControl.ps1: line 39
      at Window, C:\projects\wpfbot3000\WPFBot3000\Public\Window.ps1: line 53
      at <ScriptBlock>, C:\projects\wpfbot3000\Tests\Window.Tests.ps1: line 3

I'm using a standard build.ps1/psake/pester strategy, so if there's a way to avoid running pester in a new PowerShell session (passing -STA) I'd prefer it.

  1. 1 Posted by Mike Shepard on 26 Jul, 2018 02:32 AM

    Mike Shepard's Avatar

    I'm tagging the tests that fail in the CI build with "Local" and excluding them up here. Not ideal, but works for now.

  2. Support Staff 2 Posted by Owen McDonnell on 26 Jul, 2018 05:43 AM

    Owen McDonnell's Avatar

    Hi Mike,

    Does the test result differ when you run the same script on your local machine?

    The accepted answer to this SO post mentions a second way to change the apartment state of a powershell session. Might be worth a try.

  3. 3 Posted by Mike Shepard on 26 Jul, 2018 06:34 AM

    Mike Shepard's Avatar

    Yes, it does. The tests all pass when running locally. -STA is the
    default for PowerShell, so I'm surprised to get this specific result.

    As far as the stackoverflow suggestion, a runspace is essentially a
    powershell session, so kicking off the script in a runspace is the
    same(-sh) as starting a new PowerShell.exe. A bit nicer to deal with, so
    it might be the direction I end up going.

    Just added output to the build to show the apartment state to make sure the
    error message isn't misleading.

  4. 4 Posted by Mike Shepard on 26 Jul, 2018 06:40 AM

    Mike Shepard's Avatar

    The build shows MTA, so the error message is not misleading.

  5. 5 Posted by Mike Shepard on 04 Aug, 2018 08:01 PM

    Mike Shepard's Avatar

    Ended up kicking off a separate powershell session with PowerShell.exe -STA to run the tests and it works fine.

  6. Support Staff 6 Posted by Owen McDonnell on 05 Aug, 2018 02:33 AM

    Owen McDonnell's Avatar

    Sorry Mike, I lost track of this issue until you replied.

    Yeah, the only insight i can give is an extremely vague one - that powershell on AppVeyor build machine doesn't run the same as powershell on your local machine.
    So I think your workaround is probably best practice in this case.

  7. 7 Posted by Mike Shepard on 05 Aug, 2018 04:55 AM

    Mike Shepard's Avatar

    No worries. Just glad I have a solution.

  8. Owen McDonnell closed this discussion on 05 Aug, 2018 05:15 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

21 Aug, 2019 11:06 PM
21 Aug, 2019 02:08 PM
21 Aug, 2019 01:37 PM
21 Aug, 2019 10:21 AM
21 Aug, 2019 09:10 AM


21 Aug, 2019 05:56 AM
21 Aug, 2019 03:08 AM
20 Aug, 2019 11:33 PM
20 Aug, 2019 08:07 PM
20 Aug, 2019 05:50 PM
20 Aug, 2019 12:35 PM