第一个计时器Tick后鼠标事件不起作用

时间:2015-11-07 00:07:36

标签: powershell powershell-v2.0 powershell-v3.0

我正在使用powershell开发一个带图形界面的小工具,我对这个问题感到很疯狂......

例如: 我在表单上有一个标签,显示ping的“实时”状态。 同时,如果单击标签,则会显示一条弹出消息。

    function GoogleStatus ($Label)
    {
            $Google = "www.google.com"

            If (Test-Connection -ComputerName $Google -Count 1 -Quiet)
                    {Label.Text = "Yes"}
            else
                    {Label.Text = "No"}
    }

    function HelloMsg
    {
            [System.Windows.Forms.MessageBox]::Show("Hello","Funny Window",0,32)
    }


    [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

    #Forms
    $Form = New-Object System.Windows.Forms.Form
    $Form.Size = '150, 220' 

    $Label = New-Object System.Windows.Forms.Label  
    $Form.Controls.Add($Label)
    $Label.Location = '10, 30'
    $Label.Size = '75, 20'
    $Label.Add_Click({HelloMsg})

    #Timer Init
    $Timer = New-Object 'System.Windows.Forms.Timer'
    $Timer.Interval = 3000
    Timer.Add_Tick({GoogleStatus $Label})
    $Timer.Enabled = $True

    #Show Interface
    $Form.ShowDialog() 

在计时器的前3个秒内,单击标签会正确显示弹出消息。但如果我等待超过3秒钟,点击标签就无效了。

请帮助:(

2 个答案:

答案 0 :(得分:0)

我在代码中删除了3个拼写错误,它似乎有效(我在3秒后仍然会弹出):

function GoogleStatus ($Label) {
    $Google = "www.google.com"

    if(Test-Connection -ComputerName $Google -Count 1 -Quiet) {
        $Label.Text = "Yes" # missing $ sign here
    } else {
        $Label.Text = "No" # missing $ sign here
    }
}

function HelloMsg {
    [System.Windows.Forms.MessageBox]::Show("Hello","Funny Window",0,32)
}

[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

#Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '150, 220' 

$Label = New-Object System.Windows.Forms.Label  
$Form.Controls.Add($Label)
$Label.Location = '10, 30'
$Label.Size = '75, 20'
$Label.Add_Click({HelloMsg})

#Timer Init
$Timer = New-Object 'System.Windows.Forms.Timer'
$Timer.Interval = 3000
$Timer.Add_Tick({ GoogleStatus $Label }) # missing $ sign here
$Timer.Enabled = $True

#Show Interface
$Form.ShowDialog() 

答案 1 :(得分:0)

测试连接的超时值必须小于计时器间隔。 谢谢@Eric Walker https://stackoverflow.com/users/4270366/eric-walker