http://stackoverflow.com/questions/7350834/powershell-return-from-wait-event-when-register-objectevent-action-trigger
version 1
####### 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 ! ######"
version 2
####### Launch as : ##########################
## powershell.exe -sta -file .\balloon.ps1 ##
##############################################
Write-Host -ForeGround Yellow " ###### START OF SCRIPT ! ######"
Add-Type -Assembly System.Windows.Forms
$Title = "This is the title"
$Text = "This is the text"
$balloon = New-Object System.Windows.Forms.NotifyIcon -Property @{
Icon = [System.Drawing.SystemIcons]::Information
BalloonTipTitle = $Title
BalloonTipText = $Text
Visible = $True
}
$balloon.ShowBalloonTip(1)
$null = Register-ObjectEvent $balloon BalloonTipClicked -SourceIdentifier event_BalloonTipClicked -Action {
Write-Host -ForeGround Green "event_BalloonTipClicked occured !"
Unregister-Event -SourceIdentifier $event.SourceIdentifier -Force
Remove-Job $event.SourceIdentifier -Force
# unregister event and remove job object
Unregister-Event -SourceIdentifier event_BalloonTipClosed -Force
Remove-Job event_BalloonTipClosed -Force
$balloon.Dispose()
}
$null = Register-ObjectEvent $balloon BalloonTipClosed -SourceIdentifier event_BalloonTipClosed -Action {
Write-Host -ForeGround Green "event_BalloonTipClosed occured !"
Unregister-Event -SourceIdentifier $event.SourceIdentifier -Force
Remove-Job $event.SourceIdentifier -Force
# unregister event and remove job object
Unregister-Event -SourceIdentifier event_BalloonTipClicked -Force
Remove-Job event_BalloonTipClicked -Force
$balloon.Dispose()
}
related
###################################################
## 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 ! ######"
|