[void]
[reflection.Assembly]::LoadWithPartialName("System.ComponentModel")
$step = 0
$doWork = {
$global:step = 1
}
$runWorkerCompleted = {
$global:step = 2
}
$bg = New-Object System.ComponentModel.BackgroundWorker
$bg.add_DoWork($doWork)
$bg.add_RunWorkerCompleted($runWorkerCompleted)
$bg.RunWorkerAsync()
Start-Sleep 1 #yaya race condition. So what.
Write-Host $step
After this command on my XP SP2 using v2 CTP2, my PowerShell session dies.
What exactly are you trying to accomplish? PowerShell background jobs?
v1:
http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry
v2 CTP/CTP2: Has this built-in.
If you try to start powershell.exe from DOS, run your commands, then you
get a better indication of the error:
------------------------------
An error has occurred that was not properly handled. Additional
information is s
hown below. The Windows PowerShell process will exit.
Unhandled Exception:
System.Management.Automation.PSInvalidOperationException: T
here is no Runspace available to run scripts in this thread. You can
provide one
in the DefaultRunspace property of the
System.Management.Automation.Runspaces.R
unspace type. The script block you attempted to invoke was: $global:step = 2
...
------------------------------
Everything needs to run in a PowerShell runspace, so if you want to
background PowerShell scripts/commands, you need to run them in a new
runspace, which you need to create.
Marco
--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com
--
sapienscripter
Coming Soon: -'Managing Active Directory with Windows PowerShell: TFM'
(http://www.sapienpress.com/ad.asp)-
'[image: http://www.scriptinganswers.com/_images/logo_mvp.gif]'
(http://www.scriptinganswers.com/_images/logo_mvp.gif)
'My Blog' (http://blog.sapien.com/)
'FollowMe on Twitter' (http://www.twitter.com/JeffHicks) :cool:
-Karl
Thanks for the help.
> What exactly are you trying to accomplish? PowerShell background jobs?
Mostly I was just fumbling around and seeing what parts of .NET are
available, taking my time.
The specific job I had in mind was to delete a very large directory on
each of the servers in a grid of computers in parallel. This one-
liner got the job done
get-content PPEServer.txt | foreach { cmd /c start del /s /q \\$_\d$
\data }
I'm not sure I understand what role that plays here. The callback
lives inside the powershell runspace. Is it because it's being called
from a different thread?
GUI callbacks do work. (Maybe because a windows app runs on a single
thread?) The following code works (in v1.0), note the write-host call
in the handler. No .NET IL here. Somehow the .NET => powershell
runspace switch is being done though.
I am frustrated by the scattered / partial documentation, googling
around etc. Any links or book suggestions are welcome.
[void][reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void]
[reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$paintHandler = {
$_.Graphics.DrawString("Hello, world!",
$form.Font, [Drawing.Brushes]::Black, 0, 0)
write-host "Painting..."
}
$form = New-Object Windows.Forms.Form
$form.Text = "Paint Hello"
$form.BackColor = [Drawing.Color]::White
$form.add_Paint($paintHandler)
$form.ShowDialog()