Created
January 19, 2013 14:22
-
-
Save breezhang/4572912 to your computer and use it in GitHub Desktop.
http://pseventing.codeplex.com/
http://pseventing.codeplex.com/downloads/get/36159 some issue i fixed
Event Listening
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# updated for pseventing 1.1 | |
if ((get-pssnapin pseventing -ea silentlycontinue) -eq $null) { | |
add-pssnapin pseventing | |
} | |
function add-eventhandler ($variable, | |
$eventName , | |
[scriptblock]$script = $(throw "script cannot be null")) { | |
if ((get-pssnapin pseventing -ea silentlycontinue) -eq $null) { | |
add-pssnapin pseventing | |
} | |
if ($__handlers -eq $null) { | |
$global:__handlers = @{} | |
} | |
if ($__handlers.ContainsKey($variable)) { | |
# already contains the var | |
$__handlers[$variable].Add($eventName, $script) | |
} | |
else { | |
# new var entryw | |
$__handlers.Add($variable, @{$eventName=$script}) | |
} | |
connect-event -VariableName $variable -EventName $eventname | |
} | |
function remove-eventhandler($variable , | |
$eventName ) { | |
if ((get-pssnapin pseventing -ea silentlycontinue) -eq $null) { | |
add-pssnapin pseventing | |
} | |
if ($__handlers) { | |
disconnect-event -VariableName $variable -EventName $eventname | |
} | |
} | |
function show-eventhandler { | |
if ((get-pssnapin pseventing -ea silentlycontinue) -eq $null) { | |
add-pssnapin pseventing | |
} | |
if ($__handlers) { | |
foreach ($variable in $__handlers.Keys) { | |
$mapping = $__handlers.Item($variable) | |
$name = $variable.name | |
foreach ($eventName in $mapping.Keys) { | |
"`$$name.$eventName => $($mapping.item($eventName))" | |
} | |
} | |
} | |
} | |
function do-events($exitImmediately = $false) { | |
if ((get-pssnapin pseventing -ea silentlycontinue) -eq $null) { | |
add-pssnapin pseventing | |
} | |
$exit = $exitImmediately | |
start-keyhandler -capturectrlc | |
trap { stop-keyhandler } | |
do { | |
$events = @(read-event -wait) | |
foreach ($event in $events) { | |
if ($event.Name -eq "CtrlC") { | |
"ctrl+c hit, exiting." | |
$exit = $true | |
break | |
} | |
$var = $event.source | |
if ($var) { | |
if ($__handlers.ContainsKey($var.Name)) { | |
# call scriptblock with familiar (sender, eventargs) signature | |
$__handlers.Item($var.Name).Item($event.name).Invoke($var, $event.args) | |
} | |
} | |
} | |
} while ($exit -eq $false) | |
stop-keyhandler | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Clock =New-Object System.Timers.Timer -Property @{Interval=3000;AutoReset=$true;Enabled=$true} | |
$Clock.Start() | |
. C:\eh.ps1 | |
Add-EventHandler Clock Elapsed {"Hello World" |Out-Host} | |
$sb={ | |
$event=read-event | |
$var = $event.source | |
if ($var) { | |
if ($__handlers.ContainsKey($var.Name)) { | |
# call scriptblock with familiar (sender, eventargs) signature | |
$__handlers.Item($var.Name).Item($event.name).Invoke($var, $event.args) | |
} | |
} | |
} | |
do-events | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment