Skip to content

Instantly share code, notes, and snippets.

@Andy-AO
Created September 8, 2021 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andy-AO/e9b2949165fae074d3ad6651b762b848 to your computer and use it in GitHub Desktop.
Save Andy-AO/e9b2949165fae074d3ad6651b762b848 to your computer and use it in GitHub Desktop.
Wait-Event problem to notify click event.
[cmdletbinding()]
Param (
[string]
$Title = 'Title',
[string]
$Text = 'Text',
[scriptblock]
$OnClicked = {
'Pressed!' | Write-Host -ForegroundColor 'Blue'
}
)
# Set object properties
Add-Type -AssemblyName System.Windows.Forms
$NotifyIconForShowNotification = New-Object System.Windows.Forms.NotifyIcon
$balloon = $NotifyIconForShowNotification
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -id $pid).Path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$balloon.BalloonTipText = $Text
$balloon.BalloonTipTitle = $Title
$balloon.Visible = $true
$suffix = Get-Random
$SourceIdentifier = "click_event_$suffix"
if ($OnClicked) {
Unregister-Event -SourceIdentifier $SourceIdentifier -ErrorAction SilentlyContinue
Register-ObjectEvent $balloon BalloonTipClicked -sourceIdentifier $SourceIdentifier -Action $OnClicked
}
# Popup Notifications
$balloon.ShowBalloonTip(5000)
# Wait for click test.⚠Run this script, you will find that after clicking the notification, the event has been triggered, but the waiting is not over.
Wait-Event -timeout -1 -sourceIdentifier $SourceIdentifier
# clear
Remove-Event $SourceIdentifier -ea SilentlyContinue
Unregister-Event -SourceIdentifier $SourceIdentifier -ErrorAction SilentlyContinue
$balloon.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment