powershell - PowerShell,当 “Wait-Event”触发时从 “Register-ObjectEvent -Action”返回吗?

标签 powershell event-handling powershell-2.0 notifyicon

我已经在这个头上挠了好几个小时...下面的代码触发了单击或关闭通知气球的操作。我也是PowerShell的新手。

考虑以下代码:

####### Launch as : ##########################
## powershell.exe -sta -file .\balloon.ps1  ##
##############################################

Write-Host -ForeGround Yellow " ###### START OF SCRIPT ! ######"
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Title = "This is the title"
$Text = "This is the text"
$EventTimeOut = 5

$balloon = New-Object System.Windows.Forms.NotifyIcon
$balloon.Icon = [System.Drawing.SystemIcons]::Information
$balloon.BalloonTipTitle = $Title
$balloon.BalloonTipText = $Text
$balloon.Visible = $True

$balloon.ShowBalloonTip(1)

Register-ObjectEvent $balloon BalloonTipClicked -SourceIdentifier event_BalloonTipClicked `
    -Action {
        # explorer.exe; `
        Write-Host  -ForeGround Green "event_BalloonTipClicked occured !"; `
        # Gets rid of icon
        $balloon.Dispose(); `
    }|Out-Null

Register-ObjectEvent $balloon BalloonTipClosed -SourceIdentifier event_BalloonTipClosed `
    -Action {
        Write-Host -ForeGround Green "event_BalloonTipClosed occured !"; `
        $balloon.Dispose(); `
    }|Out-Null 

Wait-Event event_BalloonTipClicked -TimeOut $EventTimeOut
Wait-Event event_BalloonTipClosed -TimeOut $EventTimeOut

Unregister-Event -SourceIdentifier event_BalloonTipClicked
Unregister-Event -SourceIdentifier event_BalloonTipClosed

Write-Host -ForeGround Gray "Should be empty -- start --"
Get-EventSubscriber
Write-Host -ForeGround Gray "Should be empty -- end --"

#[System.Windows.Forms.MessageBox]::Show("Done !!")
Write-Host -ForeGround Yellow " ###### END OF SCRIPT ! ######"

我希望脚本在“Register-ObjectEvent”完成触发其 Action 后立即结束。

但是,返回仅在“等待事件”中指定的超时之后发生,从而阻止了代码的进一步执行。这种行为也使我无法将代码转换为函数。

如果脚本仅侦听一个事件,则其行为方式相同。

任何帮助将非常感激 !

最佳答案

您的时间的战车Shay Levy。我没有设法正确使用您的解决方案。

我使用此vbscript在后台启动文件。

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "powershell.exe -NoExit -Sta -File .\balloon.ps1",0

不幸的是,例如启动脚本5次将使我5个正在运行的Powershell实例无所作为。

我终于意识到,将“Register-ObjectEvent -Action”和“Wait-Event”混合在一起绝对是不可以的(http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/10983ec3-7aa6-4011-a87e-a30a25ab484a/)

以下代码是我的目标。这是解决问题的同步方法。
###################################################
## Launch as :                                   ##
## cmd /k powershell -Sta [-File] .\balloon.ps1  ##
###################################################


# This post put me on the right track "http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/10983ec3-7aa6-4011-a87e-a30a25ab484a/"

Write-Host -ForeGround Yellow " ###### START OF SCRIPT ! ######"
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Title = "This is the title"
$Text = "This is the text"
$EventTimeOut = 5

$balloon = New-Object System.Windows.Forms.NotifyIcon -Property @{
    Icon = [System.Drawing.SystemIcons]::Information
    BalloonTipTitle = $Title
    BalloonTipText = $Text
    Visible = $True
}

# Value "1" here is meaningless. $EventTimeOut will force bubble to close.
$balloon.ShowBalloonTip(1)

Register-ObjectEvent $balloon BalloonTipClicked -SourceIdentifier event_BalloonTipClicked
Register-ObjectEvent $balloon BalloonTipClosed -SourceIdentifier event_BalloonTipClosed

# "Wait-Event" pauses the script here until an event_BalloonTip* is triggered
# TimeOut is necessary or balloon and script hangs there forever. 
# This could be okay but event subscription gets messed up by following script instances generating the same event names1.
$retEvent = Wait-Event event_BalloonTip* -TimeOut $EventTimeOut

# Script resumes here.
$retSourceIdentifier = $retEvent.SourceIdentifier
If ($retSourceIdentifier -eq $null){
    Write-Host  -ForeGround Green "TimeOut occured !"
}Else{
    Write-Host  -ForeGround Green "$retSourceIdentifier occured !"
    }

If ($retSourceIdentifier -eq "event_BalloonTipClicked"){
    explorer.exe
    }

# Gets rid of icon. This is absolutely necessary, otherwise icon is stuck event if parent script/shell closes
$balloon.Dispose()

# Tidy up, This is needed if returning to parent shell.
Unregister-Event -SourceIdentifier event_BalloonTip*
Get-Event event_BalloonTip* | Remove-Event
Write-Host -ForeGround Gray "Should be empty -- start --"
Get-EventSubscriber
Write-Host -ForeGround Gray "Should be empty -- end --"

#[System.Windows.Forms.MessageBox]::Show("Done !!")
Write-Host -ForeGround Yellow " ###### END OF SCRIPT ! ######"

关于powershell - PowerShell,当 “Wait-Event”触发时从 “Register-ObjectEvent -Action”返回吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7350834/

相关文章:

javascript - 在 Chrome 12 中调试 Javascript 点击处理程序

powershell - 使用带有 powershell 的 newtonsoft json.net 的奇怪之处

bash - powershell bash 循环随机卡住等待键盘输入

java - 在 Java 中绘制之字折线

javascript - AngularJS:$scope.$on 和 document.addEventListener 之间的区别

powershell - 使用 PowerShell 确定具有特定文件扩展名的文件是否不在文件夹中

json - 在Powershell 2.0中读取JSON对象

powershell - 将 Remove-Item 与凭据一起使用

powershell - Azure DNS 中的通配符和裸 CNAME 记录

python - 在 Powershell 下运行 Python 脚本