Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

BackgroundWorker?

598 views
Skip to first unread message

Ramping up...

unread,
Jul 6, 2008, 12:00:14 PM7/6/08
to
Still in the early stages of learning powershell. What is wrong with
this code (besides style)? Looking for help or pointers to the right
docs.

Ramping up...

unread,
Jul 6, 2008, 12:00:14 PM7/6/08
to

Ramping up...

unread,
Jul 6, 2008, 12:04:31 PM7/6/08
to
(Actually include the code this time...)

[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

Marco Shaw [MVP]

unread,
Jul 6, 2008, 9:54:14 PM7/6/08
to

> $bg.RunWorkerAsync()

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

unread,
Jul 7, 2008, 8:41:28 AM7/7/08
to

You're code still isn't showing.


--
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 Prosser[MVP]

unread,
Jul 7, 2008, 1:07:32 PM7/7/08
to
Background worker is for running DOTNET code on a background thread.
Powershell, though a dotnet centered language doesn't produce DOTNET CLR
IL. Its an interpreted language and its code cannot run outside of the
context of a powershell runspace. If you want to do this you have to
create a PowerShell Runspace and run your script there, and marshall the
results back to your primary session. There are a number of third
party scripts out there that do this.

-Karl

Ramping up...

unread,
Jul 7, 2008, 4:44:12 PM7/7/08
to
On Jul 6, 6:54 pm, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com>
wrote:


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 }

Ramping up...

unread,
Jul 7, 2008, 5:35:34 PM7/7/08
to
On Jul 7, 10:07 am, "Karl Prosser[MVP]" <karl@p_o_w_e_r_s_h_e_l_l.com>
wrote:

> Background worker is for running DOTNET code on a background thread.
> Powershell, though a dotnet centered language doesn't produce DOTNET CLR
>   IL. Its an interpreted language and its code cannot run outside of the
> context of a powershell runspace. If you want to do this you have to
> create a PowerShell Runspace and run your script there, and marshall the
>   results back to your primary session. There are a number of third
> party scripts out there that do this.
>
> -Karl

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()

0 new messages