PS C:\Users\bob> $timer | Get-Member -type Event
TypeName: System.Timers.Timer
Name MemberType Definition
---- ---------- ----------
Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs)
Elapsed Event System.Timers.ElapsedEventHandler Elapsed(System.Object, System.Timers.ElapsedEventArgs)
#manually check if timer event fires
PS C:\Users\bob> Register-ObjectEvent -InputObject $timer -EventName Elapsed
PS C:\Users\bob> Get-Event
PS C:\Users\bob> $timer.Interval = 5000
PS C:\Users\bob> $timer.AutoReset = $false
PS C:\Users\bob> $timer.Enabled = $true
PS C:\Users\bob> Get-Event
ComputerName :
RunspaceId : da9405ad-7c81-49cb-aa86-2dae09280e3c
EventIdentifier : 1
Sender : System.Timers.Timer
SourceEventArgs : System.Timers.ElapsedEventArgs
SourceArgs : {System.Timers.Timer, System.Timers.ElapsedEventArgs}
SourceIdentifier : f27b201f-c07d-4566-8a3c-7965f88d8ec3
TimeGenerated : 2020-08-23 1:40:15 PM
MessageData :
PS C:\Users\bob> Get-Event | Remove-Event
PS C:\Users\bob> Get-EventSubscriber
SubscriptionId : 1
SourceObject : System.Timers.Timer
EventName : Elapsed
SourceIdentifier : f27b201f-c07d-4566-8a3c-7965f88d8ec3
Action :
HandlerDelegate :
SupportEvent : False
ForwardEvent : False
PS C:\Users\bob> Get-EventSubscriber | Unregister-Event
#synchronously wait for event to fire, user control is disabled while waiting
PS C:\Users\bob> Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier "WaitingForTimer"
PS C:\Users\bob>
PS C:\Users\bob> $timer.Interval = 10000
PS C:\Users\bob> $timer.AutoReset = $false
PS C:\Users\bob> $timer.Enabled = $true
PS C:\Users\bob> Wait-Event "WaitingForTimer"
ComputerName :
RunspaceId : da9405ad-7c81-49cb-aa86-2dae09280e3c
EventIdentifier : 2
Sender : System.Timers.Timer
SourceEventArgs : System.Timers.ElapsedEventArgs
SourceArgs : {System.Timers.Timer, System.Timers.ElapsedEventArgs}
SourceIdentifier : WaitingForTimer
TimeGenerated : 2020-08-23 1:50:41 PM
MessageData :
Get-Event | Remove-Event
Get-EventSubscriber | Unregister-Event
#asynchronously fire event, while waiting for timer, other task can be done
PS C:\Users\bob> $timer.Interval = 2500
PS C:\Users\bob> $timer.AutoReset = $true
PS C:\Users\bob> $timer.Enabled = $true
PS C:\Users\bob>
PS C:\Users\bob> Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier "Async Timer" -Action {Write-Host "Timer fired at " ([System.DateTime]::Now)}
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Async Timer NotStarted False Write-Host "Timer fired …
PS C:\Users\bob> Timer fired at 2020-08-23 2:00:37 PM
Timer fired at 2020-08-23 2:00:40 PM
Timer fired at 2020-08-23 2:00:42 PM
...
get-event
Timer fired at 2020-08-23 2:00:57 PM
...
Get-EventSubscriber
SubscriptionId : 3
SourceObject : System.Timers.Timer
EventName : Elapsed
SourceIdentifier : Async Timer
Action : System.Management.Automation.PSEventJob
HandlerDelegate :
SupportEvent : False
ForwardEvent : False
...
PS C:\Users\bob> Timer fired at 2020-08-23 2:07:57 PM
Timer fired at 2020-08-23 2:07:59 PM
Timer fired at 2020-08-23 2:08:02 PM
> Unregister-Event -SourceIdentifier "Async Timer"
PS C:\Users\bob>
reference:
No comments:
Post a Comment