【问题标题】:Windows.Forms.Timer scope [duplicate]Windows.Forms.Timer 范围
【发布时间】:2020-10-13 22:03:42
【问题描述】:
$timer = [Windows.Forms.Timer]::new()
$timer.Interval = 1000
$b=5
function tick {
    write-host ($b++)
}
$timer.add_tick({ tick })

$Form = [Windows.Forms.Form]::new()

$Timer.Start()
$Form.Showdialog()

输出总是 5。如何让计时器能够从全局范围内看到变量?

【问题讨论】:

    标签: .net powershell


    【解决方案1】:

    您可以使用脚本范围修饰符

    $timer = [Windows.Forms.Timer]::new()
    $timer.Interval = 1000
    $script:b=5
    function tick {
        write-host ($script:b++)
    }
    $timer.add_tick({ tick })
    
    $Form = [Windows.Forms.Form]::new()
    
    $Timer.Start()
    $Form.Showdialog()
    

    更多信息可以在这里找到about_scopes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      相关资源
      最近更新 更多