Created
September 8, 2021 23:47
-
-
Save Andy-AO/e9b2949165fae074d3ad6651b762b848 to your computer and use it in GitHub Desktop.
Wait-Event problem to notify click event.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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