Change Set 432c495144fc error

Oct 13, 2010 at 4:16 PM

Hi,

I've just downloaded Change Set 432c495144fc, it successfully imports but when I try the first example on http://huddledmasses.org/powerboots-tutorial-walkthrough/ 

I get the following error:

PS C:\Users\administrator.RAVENHOLM> New-BootsWindow -SizeToContent widthandheig
ht -content (button -content "hello world" )
New-Object : Exception calling ".ctor" with "0" argument(s): "The calling thread must be STA, because many UI components require this."
At line:403 char:25
+    $DObject = New-Object <<<<  System.Windows.Controls.Button
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvoca
   tionException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
   Shell.Commands.NewObjectCommand

[ref] cannot be applied to a variable that does not exist.
At line:418 char:161
+ Set-PowerBootsProperties @($PSBoundParameters.GetEnumerator() | Where { [Arra
y]::BinarySearch($All,($_.Key -replace "^On_(.*)",'$1__')) -ge 0 } ) ([ref]$DOb
ject <<<< )
    + CategoryInfo          : InvalidOperation: (DObject:Token) [], RuntimeExc
   eption
    + FullyQualifiedErrorId : NonExistingVariableReference
I throught the new version of this module worked without the STA flag?

regards

Jon

Coordinator
Oct 13, 2010 at 6:03 PM
Edited Oct 13, 2010 at 6:43 PM

That's expected, sorry. Not so much a bug, as a bad choice for the first example.

The problem was that the first example used parentheses, so the "Button" command tries to run in the PowerShell host and it's output (the actual button) would be passed into the Content parameter.  The problem is that of all the cmdlets and functions that create UI, only New-BootsWindow actually works in MTA mode -- it creates a STA thread for the UI, and then all the other commands (like "Button") need to be in a script block that's passed to the New-BootsWindow function so they can run on that UI thread.

I've updated the walk-through to explain that a little bit, but basically, in STA mode you have to write it like:

New-BootsWindow -SizeToContent WidthAndHeight -Content {
   Button -Content "Push Me"
}

Using braces instead of parenthesis turns that into a scriptblock which will be executed on the UI thread, instead of an expression which will be evaluated and the output passed to New-BootsWindow.