立即执行计时器动作而不等待Timer.interval[Powershell3]

问题描述

当前正在执行基于计时器的操作,在指定的计时器间隔后激活该操作。

我想要一个基于计时器的重复动作,但是当将其设置为更长的时间间隔(例如1小时)等时,必须有一种方法来测试动作块是否正确执行而不必等待整个时间间隔。

是否可以立即激活动作,而不必等到Timer.interval之后开始执行?

***Console***

 17-08-2020 07:30:18 Script_Started
    
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
4      Timer.Elapsed                   NotStarted    False                                 Write-Host  $n


17-08-2020 07:30:28 Action block executed after Interval = 10000  
17-08-2020 07:30:38 Action block executed after Interval = 10000  



***Script***   
$Timer = New-Object System.Timers.Timer
$objectEventArgs = @{
    InputObject = $Timer
    EventName = 'Elapsed'
    SourceIdentifier = 'Timer.Elapsed'
}

$Timer.Interval = 1000 * 10 
$Timer.Autoreset = $True
$Timer.Enabled = $True


$global:n = "  Action block executed after Interval = " +  $Timer.Interval


Write-Host $((Get-Date).ToString() + "  Script_Started")

$action = { Write-Host  $(Get-Date).ToString() + $n } 

Register-ObjectEvent @objectEventArgs -Action $action


#Unregister-Event  -SourceIdentifier 'Timer.Elapsed' 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)