.net Powershell-处理Windows窗体事件

.net Powershell-处理Windows窗体事件,.net,winforms,powershell,.net,Winforms,Powershell,作为一名后端脚本编写人员/devops人员,我对.net的熟悉程度不如我在jr软件工程师时那么多。然而,我喜欢度量标准,并且有了为我的vpn、cpu和ram使用情况编写网络监视器的想法,因为某些站点导致google泄漏内存,直到我的cpu和ram使用率达到99%时,我才注意到这一点 所以我有了这个脚本,我决定也尝试制作一个gui,因为大家都知道,现在每个人都想要一个完整的堆栈开发人员 在我的一生中,我无法找到如何让计时器和事件对象在表单加载时启动,以创建我的准无限循环 我一直在使用source来

作为一名后端脚本编写人员/devops人员,我对.net的熟悉程度不如我在jr软件工程师时那么多。然而,我喜欢度量标准,并且有了为我的vpn、cpu和ram使用情况编写网络监视器的想法,因为某些站点导致google泄漏内存,直到我的cpu和ram使用率达到99%时,我才注意到这一点

所以我有了这个脚本,我决定也尝试制作一个gui,因为大家都知道,现在每个人都想要一个完整的堆栈开发人员

在我的一生中,我无法找到如何让计时器和事件对象在表单加载时启动,以创建我的准无限循环

我一直在使用source来了解事件计时器和注册的objectevent

########################################################################
# Modified by Derek from source below. All I did was the psexec query and cleaned it up a bit.
# Original basis for gui updating created by:
# Code Generated By: SAPIEN Technologies, Inc., PrimalForms 2009 v1.1.10.0
# Generated On: 23/10/2010 08:37
# Generated By: KST
# http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/780aaa2e-4edf-495b-a63e-d8876eab9b25
########################################################################
function OnApplicationLoad {
    return $true 
}
function OnApplicationExit {
    $script:ExitCode = 0 
}
function Get-ComputerStats {
    process {

        $avg = Get-WmiObject win32_processor | 
            Measure-Object -property LoadPercentage -Average | 
            Foreach {$_.Average}
        $mem = Get-WmiObject win32_operatingsystem |
            Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) * 100) / $_.TotalVisibleMemorySize)}
        $free = Get-WmiObject Win32_Volume -Filter "DriveLetter = 'C:'" |
            Foreach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity) * 100)}
        [pscustomobject] [ordered] @{ 
            ComputerName = $env:computername
            AverageCpu   = $avg
            MemoryUsage  = $mem
            PercentFree  = $free
        }
    }
}
function GenerateForm {
    [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    [void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form = New-Object System.Windows.Forms.Form
    $button = New-Object System.Windows.Forms.Button
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

    $formEvent_Load = {
        $Timer = New-Object -Type Timers.Timer
        $Timer.Interval = 30000
        $timer.AutoReset = $true
        $timeout = 0

        $handler = {
            $pySpeedDir = "C:\Users\CentralTerminal\AppData\Local\Programs\Python\Python36\Scripts\pyspeedtest.exe"
            $speed = & $pySpeedDir
            $table = Get-ComputerStats 
            $outputBox.Text += get-date
            $outputBox.Text += "`n"
            $outputBox.Text += $speed
            $outputBox.Text += $table
            $outputBox.Select()
            $outputBox.SelectionStart = $outputBox.Text.Length
            $outputBox.ScrollToCaret()
            $outputBox.Refresh()
            $Timer.Stop()
            sleep -s 1  
        }

        $start = Register-ObjectEvent -InputObject $timer -SourceIdentifier TimerElapsed -EventName Elapsed -Action $handler
        $Timer.Start()
    }


    $form_StateCorrection_Load =
    {
        $form.WindowState = $InitialFormWindowState
    }


    $form.Controls.Add($outputBox)
    $form.Text = "Network and Machine Load"
    $form.Name = "GUM"
    $form.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation 
    $form.ClientSize = New-Object System.Drawing.Size(800, 400)
    #$Icon = New-Object system.drawing.icon ("brandimage.ICO")
    #$form.Icon = $Icon
    #$Image = [system.drawing.image]::FromFile("poweredbyit.jpg")
    #$form.BackgroundImage = $Image
    $form.BackgroundImageLayout = "None"
    $form.add_Load($formEvent_Load)


    $outputBox.Name = "outputBox"
    $outputBox.Text = ""
    $outputBox.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation 
    $outputBox.Location = New-Object System.Drawing.Point(5, 35)
    $outputBox.Size = New-Object System.Drawing.Size(785, 320) 
    $outputBox.font = "lucida console"
    $outputBox.TabIndex = 0

    $InitialFormWindowState = $form.WindowState
    $form.add_Load($form_StateCorrection_Load)
    return $form.ShowDialog()
} 

if (OnApplicationLoad -eq $true) {
    GenerateForm | Out-Null
    OnApplicationExit
}

我已经有一段时间没有发帖了,所以我为糟糕的格式道歉。感谢您查看

为GUI对象注册事件,您可以使用以下选项之一:

•备选方案1

•备选方案2

•备选方案3

示例

如果要从计时器事件更新GUI,请使用
System.Windows.Forms.Timer
并处理其
Tick
事件:

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Size = "400, 400"
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
Function Timer_Tick()
{
    $form.Text = Get-Date -Format "HH:mm:ss"
}
Function Form_Load
{
    $form.Text = "Timer started"
    $timer.Start()
}
$form.Add_Load({Form_Load})
$timer.Add_Tick({Timer_Tick})
$form.ShowDialog()

谢谢你的回答。这起到了作用,目前正在发挥作用。标记为已回答
$Form_Load = {$form.Text = "Form Load Event Handled!"}    
$form.Add_Load($Form_Load)
$form.Add_Load({Form_Load})
Function Form_Load
{
    $form.Text = "Form Load Event Handled!"
}
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Size = "400, 400"
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
Function Timer_Tick()
{
    $form.Text = Get-Date -Format "HH:mm:ss"
}
Function Form_Load
{
    $form.Text = "Timer started"
    $timer.Start()
}
$form.Add_Load({Form_Load})
$timer.Add_Tick({Timer_Tick})
$form.ShowDialog()