You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell使用Timers.Timer时计时器停止后脚本卡死如何修复?

问题根因
  • 主循环无退出逻辑:脚本中的while($true)为永久死循环,即使计时器停止、事件被注销,主循环仍会无限执行打印逻辑,不会主动结束,表现为脚本无响应卡死。
  • 变量作用域不匹配:通过Register-ObjectEvent注册的回调脚本块$action运行在独立的事件上下文,默认无法直接访问主脚本作用域的$complete、$timer等变量,不加作用域修饰的情况下,回调内的判断逻辑要么无法触发,要么读取到无效空值。
修复方案

对脚本做以下三处修改即可解决问题:

  1. 新增主循环运行开关变量,替换硬编码的$true循环条件,回调触发停止逻辑时修改开关值,让主循环可以正常退出
  2. 所有需要在回调内访问、修改的主脚本变量,统一加$script:前缀指定脚本作用域,确保跨上下文变量读写生效
  3. 将事件注销、计时器资源释放逻辑移到主循环结束后执行,避免事件回调线程与主线程的资源操作冲突

修复后的完整代码如下:

Clear-Host
$script:timer = [Timers.Timer]::new()
$timer.Interval = 200
# 新增主循环运行开关
$script:running = $true
$script:complete = 0

$action = {
    if($script:complete -eq 8){
        write-host "completed $script:complete" -ForegroundColor Red
        write-host "Timer stopped" -ForegroundColor Red
        $script:timer.Stop()
        # 修改开关终止主循环
        $script:running = $false
    }
}
Register-ObjectEvent -InputObject $timer -EventName Elapsed `
-SourceIdentifier thetimer -Action $action | Out-Null

$timer.Start()

# 用开关变量控制主循环运行
while($script:running){
    Write-Host "Script working: $script:complete" -ForegroundColor Green
    Start-Sleep -Milliseconds 400
    $script:complete++
}

# 主循环退出后统一清理资源
Unregister-Event thetimer
$timer.Dispose()
Write-Host "Script exited normally" -ForegroundColor Cyan

内容的提问来源于stack exchange,提问作者Danny。

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 02:15:04