从另一个PowerShell运行空间管理对象和变量 [英] manage objects and variables from another PowerShell runspace

查看:150
本文介绍了从另一个PowerShell运行空间管理对象和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working with the System.Timers.Timer object in Powershell.

我遇到的问题是,当您注册到已逝去"事件时

The Problem i have encountered is, that when you register to the "Elapsed" event

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

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

the scriptblock will run in a different Powershell Runspace / thread The problem here is, that i want to modify the timer object in the scriptblock and basically create a loop of shorter Intervals until it reaches x seconds and the script stops

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

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

I have found the option to define runspaces, but i am not sure if i can work with the timer object through a self defined runspace. Also i think using runspaces is a bit over the top for this task.

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

Is there another (simpler) way to get this to work? If not, is it possible to manipulate the timer object through self defined runspaces? (i probably do this in a different way if i'd have to use runspaces, but it is nice to know for the future)

推荐答案

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

The $timer itself is passed along as the first argument to the event handler as the sender. This populates the $Sender automatic variable inside the action block automatically.

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

You can modify that object reference instead of dereferencing $timer directly:

# 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自动变量):

You can also override the variable names by defining a param block inside the Action scriptblock, first argument is always the sender, second argument is the EventArgs (equivalent to the $EventArgs auto variable):

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

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

这篇关于从另一个PowerShell运行空间管理对象和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆