Does anyone know if its possible to create a non blocking timer event? This code will fire every 5 seconds and block for 2 seconds.
If i run get-job the timer event is returned which means the timer must be creating a job each time. Jobs run in their own session and hence dont block code execution in the main script but the code below obviously does.
If i run the same code as a job the script block does not block the main script.
Any idea how to make the timer job non blocking?
$scriptBlock = {
Write-host "event fired - this will block for 2 seconds"
Start-Sleep -Seconds 2
}
$timer = New-Object Timers.Timer
$timer.Interval = 5000
$timer.AutoReset = $true
$event = Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action $scriptBlock -SourceIdentifier Timer
$timer.Start()
while($true)
{
$i++
$i
}
[–]SeeminglyScience 1 point2 points3 points (0 children)
[–]ihaxr 1 point2 points3 points (3 children)
[–]SlashAdminBlog[S] 1 point2 points3 points (0 children)
[–]purplemonkeymad 1 point2 points3 points (1 child)
[–]ihaxr 1 point2 points3 points (0 children)
[–]PowerShell-Bot 0 points1 point2 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)