2015-11-07 51 views
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秒鐘,則點擊標籤不起作用。

幫助請:(

+0

請幫助!:( – Nick32342

回答

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

謝謝您的幫助。我發現這個問題,但我不知道如何解決它在我的真實代碼中,函數GoogleStatus嘗試ping另一個主機名並花費很長時間。鼠標事件不起作用,因爲腳本處於等待狀態。你知道我該如何解決它? – Nick32342

+0

測試連接的超時值必須小於您的計時器間隔。 –

+0

嘿傢伙, 如果你已經準備好幫助,我已經在這裏發佈了我的整個問題:) Thx http://stackoverflow.com/questions/33639213/background-job-to-refresh-data-on-a - 形式,而-腳本是運行的 – Nick32342

相關問題