PoshCode Archive  Artifact [21db4c2a42]

Artifact 21db4c2a42a94628853eec09d8fc888f9765b1b5ae7d0d8a078b9c70ba76cfc5:

  • File Register-Timer.ps1 — part of check-in [14b09eb44a] at 2018-06-10 13:59:59 on branch trunk — The equivalent of SetTimer/setInterval/setTimeout in PowerShell. (user: Public Domain size: 1204)

# encoding: ascii
# api: powershell
# title: Register-Timer
# description: The equivalent of SetTimer/setInterval/setTimeout in PowerShell.
# version: 1000.0
# type: function
# author: Public Domain
# license: CC0
# function: Register-Timer
# x-poshcode-id: 5718
# x-archived: 2016-05-19T09:39:25
# x-published: 2016-01-28T05:58:00
#
#
function Register-Timer {
	[CmdletBinding()]
	param(
		[Parameter(Mandatory, Position = 0)]
		[ValidateRange(0.00050000000000000012, 2147483.6474999995)]
		[double]$TimeoutSec
,
		[Parameter(Mandatory, Position = 1)]
		[scriptblock]$Action
,
		[string]$SourceIdentifier
,
#.Parameter SupportEvent
#  Use this to suppress job creation
		[switch]$SupportEvent
,
#.Parameter MessageData
#  Access it via $event.MessageData
		$MessageData = $null
,
		[int]$Count = 1
	)
	$t = [Timers.Timer][int](1000.0*$TimeoutSec)
	$extra = @{}
	if ($PSBoundParameters.ContainsKey('SourceIdentifier')) {
		$extra['SourceIdentifier'] = $SourceIdentifier
	}
	$ret = Register-ObjectEvent -InputObject $t -EventName Elapsed -Action $Action -MessageData $MessageData -MaxTriggerCount $Count -SupportEvent: $SupportEvent @extra
	$t.Start()
	$ret
}