繁体   English   中英

从另一个PowerShell运行空间管理对象和变量

[英]manage objects and variables from another PowerShell runspace

我目前正在使用Powershell中的System.Timers.Timer对象。

我遇到的问题是,当您注册到“ Elapsed”事件时

Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier ThirtySecTimer -Action $scriptblock

脚本块将在不同的Powershell运行空间/线程中运行这里的问题是,我想修改脚本块中的计时器对象,并基本上创建一个较短间隔的循环,直到达到x秒并且脚本停止

$action={
    $timer.stop()
    Show-MsgBox -Prompt "time's up" #selfdefined function using Windows Forms
    $timer.interval=$interval - 1000
    $timer.start()
}

我已经找到了定义运行空间的选项,但是我不确定是否可以通过自定义运行空间使用计时器对象。 我也认为使用运行空间在此任务的顶部有点过头。

还有另一种(更简单的)方法可以使它起作用吗? 如果不是,是否可以通过自定义运行空间来操作计时器对象? (如果我必须使用运行空间,我可能会以其他方式执行此操作,但是很高兴知道将来)

$timer本身作为第一个参数传递给事件处理程序,作为sender 这将自动在操作块内填充$Sender自动变量。

您可以修改该对象引用,而不是直接取消引用$timer

# Create a timer starting at a 10 second interval
$timer = New-Object System.Timers.Timer
$timer.Interval = 10000

# Register the event handler
Register-ObjectEvent $timer Elapsed timersourceid -Action {
    Write-Host "Event triggered at $(Get-Date|Select-Object -ExpandProperty TimeOfDay)"

    $Sender.Stop()
    if($Sender.Interval -ge 1000)
    {
        $Sender.Interval = $Sender.Interval - 1000
        $Sender.Start()
    }
    else
    {
        Write-Host "Timer stopped"
    }
}

您还可以通过在Action脚本块中定义一个param块来覆盖变量名称,第一个参数始终是发送者,第二个参数是EventArgs(相当于$EventArgs自动变量):

Register-ObjectEvent $Timer Elapsed SourceId -Action {
    param($s,$e)

    $s.Stop()
    Write-Host "Event was raised at $($e.SignalTime)"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM