【问题标题】:Mouse event don't work after first timer Tick鼠标事件在第一个计时器 Tick 后不起作用
【发布时间】:2015-11-11 04:05:27
【问题描述】:

我正在使用 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 秒,点击标签是没有效果的。

请帮忙:(

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    我已经删除了代码中的 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() 
    

    【讨论】:

    • 感谢您的帮助。我找到了问题,但我不知道如何解决它。在我的真实代码中,函数 GoogleStatus 尝试 ping 另一个主机名,它需要很长时间。鼠标事件不起作用,因为脚本处于等待状态。你知道我该如何解决吗?
    • 测试连接的超时值必须小于你的定时器间隔。
    • 嘿伙计们,如果你愿意帮忙,我已经把我的整个问题贴在这里了 :) 谢谢stackoverflow.com/questions/33639213/…
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2011-02-02
      • 2015-06-23
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多