Combobox is empty when running it –AsJob

Sep 27, 2013 at 7:46 PM
This is a very simple example, my program is much larger. If I run the script below with –show my combobox is populated with information, when I run it with –AsJob it is empty. I can’t figure out why, can this be explained please?
$ItemList = "Item1","Item2","Item3","Item4"

New-Stackpanel -Name Info -Orientation Horizontal {
            
    New-Label -Row 0 -Column 0 'Select Item'
    New-ComboBox -Row 0 -Column 2 -Width 130 -IsTextSearchEnabled:$true -Name strItemSelected -Items $ItemList -SelectedIndex 0

} -AsJob
Regards,

Mike
Coordinator
Sep 30, 2013 at 6:32 PM
Well, in this example, the simple answer is that when you use -AsJob it's the same as when you run other Jobs in PowerShell (e.g. Invoke-Command -AsJob): the new job is in a new Runspace (in a new thread or even a new process), and doesn't have access to variables that are defined outside it ...
Marked as answer by Jaykul on 11/14/2013 at 2:56 PM