简体   繁体   English

连续Ping Powershell表格

[英]Continuous Ping Powershell Form

OK so I am newer to using windows forms inside Powershell, and I am having a bit of trouble with the form being all laggy since there is a continuous ping running. 好的,所以我刚开始在Powershell中使用Windows窗体,由于连续运行ping操作,窗体比较笨拙,我有点麻烦。 Fundamentally all I need to do is display the IP address each time it runs a ping. 从根本上讲,我需要做的就是每次运行ping时显示IP地址。 I tried to search around and it seems like the proper way to do this is with a running job in the background? 我试图四处搜索,看来执行此操作的正确方法是在后台运行作业?

I think the way this is currently written that the ping happens in the background but I'm not sure how to update the form (or if its even possible to make it visible to it?) 我认为当前编写这种方式的方式是在后台执行ping操作,但是我不确定如何更新表单(或者是否有可能使它可见)?

Here is a sample, any guidance with this would be greatly appreciated. 这是一个示例,对此的任何指导将不胜感激。

function global:ContinuousPing{
    $global:job = start-job {
        while($true){
            $pingStatus = Test-Connection google.com -Count 1
            $label.text = $pingStatus.IPV4Address.IPAddressToString
            #[System.Windows.Forms.Application]::DoEvents()
            start-sleep 1
        }
    }
}

Add-Type -AssemblyName System.Windows.Forms

$pingForm = New-Object System.Windows.Forms.Form
$label = New-Object System.Windows.Forms.Label
$button1 = New-Object System.Windows.Forms.Button

$ping = {
    ContinuousPing

    while($true){
        Receive-Job $job
    }
}

$label.Text = "Ping Status"

$button1.Location = New-Object System.Drawing.Point(100,10)
$button1.add_Click($ping)

$pingForm.Controls.Add($label)
$pingForm.Controls.Add($button1)

$pingForm.ShowDialog() | Out-Null

remove-job $job

You need to separate the GUI updating and the ping task. 您需要将GUI更新和ping任务分开。 This is easy to in languages that are designed to have GUIs like C# winforms, If you tried it you would be surprised how much easier it is than trying to bend a GUI over PowerShell. 对于设计为具有C#winforms等GUI的语言来说,这很容易。如果尝试使用它,您会惊讶地发现它比在PowerShell上弯曲GUI容易得多。

You can try the link Mathias posted, It will do what you require. 您可以尝试Mathias发布的链接,它将完成您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM