Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass variable to register-objectevent action block

Tags:

powershell

I'm actually trying to run a timer (lmd) every 50 sec once. but when i press ctrl+c the timer still continues to print the message in its action block. So i wrote an other timer (ctrlc) which runs every sec and looks for any ctrlc pressed. Then unregisters the lmd timer and ctrl timer both. and resets the TreatControlCAsInput to false. But i have a problem inside the ctrlc timer action block. i'm not able to trigger the RemoveTimer function. Please suggest :)

# Disables the timer and unregisters the event subscriber
function RemoveTimer ($timerin, $sourceidentifier) {
     Write-Host "Inside RemoveTimer $sourceidentifier"
    try {
        $timerin.Enabled = $False
        Unregister-Event -SourceIdentifier $sourceidentifier
    } Catch {
    Write-Host "Error occurred while stopping timer $($_.Exception.Message)"
}
}

# Disables the timer and unregisters the event subscriber
function RemoveTimer1 {
    Write-Host "Inside RemoveTimer1"
}

# LMD Timer
try {
    $lmdtimer = New-Object Timers.Timer
    $lmdtimer.Interval = 50000
    $registerevent = Register-ObjectEvent -InputObject $lmdtimer -EventName Elapsed -SourceIdentifier LMDTimer.Output -Action {
        Write-Debug "$($Event | Out-String)"
        Write-Host "Script is running"
    }
    $lmdtimer.Enabled = $True
    Write-Debug "$($lmdtimer | Out-String)"
} Catch {
    Write-Host "Error occurred while starting LMDTimer $($_.Exception.Message)"
    RemoveTimer $lmdtimer "LMDTimer.Output"
}

# CTRLC Timer
try {
    [console]::TreatControlCAsInput = $true
    $ctrlctimer = New-Object Timers.Timer
    $ctrlctimer.Interval = 1000
    $params = new-object psobject -property @{RemoveTimer1 = $function:RemoveTimer1}
    $registerevent = Register-ObjectEvent -InputObject $ctrlctimer -EventName Elapsed -SourceIdentifier CTRLCTimer.Output -MessageData $function:RemoveTimer1 -Action {
        if ($Host.UI.RawUI.KeyAvailable -and (3 -eq [int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character))
        {
          Write-Host "Removing LMDTimer and CTRLCTimer"
          try {
              #RemoveTimer $lmdtimer "LMDTimer.Output"
              #RemoveTimer $ctrlctimer "CTRLCTimer.Output"
              $Event.MessageData.RemoveTimer1
          } Catch {
              Write-Host $_.Exception.Message
          }
          Write-Host "Removing TreatControlCAsInput"
          [console]::TreatControlCAsInput = $false
          exit
        }
    }
    $ctrlctimer.Enabled = $True
    Write-Debug "$($ctrlctimer | Out-String)"
} Catch {
    Write-Host "Error occurred while starting CTRLCTimer $($_.Exception.Message)"
    RemoveTimer $ctrlctimer "CTRLCTimer.Output"
    [console]::TreatControlCAsInput = $false
}

for ($i=0;$i -lt 4000;$i++) {
    Write-Host "$i"
}

RemoveTimer $lmdtimer "LMDTimer.Output"
RemoveTimer $ctrlctimer "CTRLCTimer.Output"
[console]::TreatControlCAsInput = $false
like image 310
Sobhitha Neelanath Avatar asked May 21 '15 07:05

Sobhitha Neelanath


1 Answers

You can use the -MessageData parameter to pass info to the scriptblock:

$pso = new-object psobject -property @{foo = $foo; bar = $bar}
Register-ObjectEvent... -messagedata $pso

After that you should be able to access it inside the Scriptblock like this:

$Event.MessageData.foo
like image 193
Paul Avatar answered Oct 10 '22 06:10

Paul