0

I'm using powershell to develop a little tool with graphic interface and I'm going crazy with this issue...

For example: I have a label on a form that display "real-time" status of a ping. In the same time, if you click on the label, a popup message is displayed.

    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() 

During the first 3 secondes of the timer, clicking on the label display correctly the popup message. But if I wait more than 3 seconds, clicking on the label has no effect.

Help please :(

Nick32342
  • 93
  • 1
  • 7

2 Answers2

0

I have removed 3 typos in the code, and it seems to work (I still get the popup after 3 seconds have elapsed) :

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() 
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • Thank you for your help. I've found the problem but I don't know how to fix it. In my real code, the function GoogleStatus try to ping another hostname and it take to long. The mouse event don't work because the script is on wait satus. Do you know how can I adress it? – Nick32342 Nov 08 '15 at 19:28
  • The timeout value for test-connection has to be less than your timer interval. – Eric Walker Nov 09 '15 at 00:21
  • Hey guys, I've posted my entire problem here if you're ready to help :) Thx http://stackoverflow.com/questions/33639213/background-job-to-refresh-data-on-a-form-while-script-is-running – Nick32342 Nov 10 '15 at 20:49
0

The timeout value for test-connection has to be less than your timer interval. Thank you @Eric Walker https://stackoverflow.com/users/4270366/eric-walker

Community
  • 1
  • 1
Nick32342
  • 93
  • 1
  • 7