Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New closure on scriptblock

Consider this code:

PS> $timer = New-Object Timers.Timer
PS> $timer.Interval = 1000
PS> $i = 1;
PS> Register-ObjectEvent $timer Elapsed -Action { write-host 'i: ' $i }.GetNewClosure()
PS> $timer.Enabled = 1
i:  1
i:  1
i:  1
 ...
# wait a couple of seconds and change $i
PS> $i = 2
i:  2
i:  2
i:  2

I assumed that when I create new closure ({ write-host 'i: ' $i }.GetNewClosure()) value of $i will be tied to this closure. But not in this case. Afer I change the value, write-host takes the new value.

On the other side, this works:

PS> $i = 1;
PS> $action = { write-host 'i: ' $i }.GetNewClosure()
PS> &$action
i:  1
PS> $i = 2
PS> &$action
i:  1

Why it doesn't work with the Register-ObjectEvent?

like image 927
stej Avatar asked Mar 10 '10 09:03

stej


1 Answers

Jobs are executed in a dynamic module; modules have isolated sessionstate, and share access to globals. PowerShell closures only work within the same sessionstate / scope chain. Annoying, yes.

-Oisin

p.s. I say "jobs" because event handlers are effectively local jobs, no different than script being run with start-job (local machine only, implicit, not using -computer localhost)

like image 181
x0n Avatar answered Nov 07 '22 04:11

x0n