【问题标题】:Stop-Job Global:job isn't defined as an ID or value全局停止作业:作业未定义为 ID 或值
【发布时间】:2021-10-27 16:23:34
【问题描述】:

这是一个基本的 ping 工具,经过了许多更改,但由于某种原因,它代表第一个按钮下的停止功能,因此未定义并且不允许进程停止。

  Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$job = $null
$isRunning = $false


$form = New-Object System.Windows.Forms.Form
$form.Text = 'Clevagroup Pinger'
$form.Size = New-Object System.Drawing.Size(350,150)
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true


$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Ping"
$Button.Add_Click({
    if($global:isRunning -eq $false){
       $global:job = Start-Job -ScriptBlock {Ping 8.8.8.8 -t > $env:userprofile\desktop\PingResults}
       $Button.Text = "Running"
       $global:isRunning = $true
    } else {
            $Button.Text = "Stop Pinging"
            Stop-Job $global:job 
            $global:isRunning = $false
    }



   
})
$Form.Controls.Add($Button)


$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(195,35)
$Button1.Size = New-Object System.Drawing.Size(120,23)
$Button1.Text = "Close"
$Button1.Add_Click({

    if($global:job -ne $null){
        Stop-Job $global:job
    }

    
                      
})

$Form.Controls.Add($Button1)

$form.Add_Shown({$Button.Select()})
$result = $form.ShowDialog()

感谢您提供的任何帮助。

【问题讨论】:

  • 所以......该错误在什么时候出现?该错误的确切文本是什么? 请将完整的错误文本添加到您的问题中,以便人们轻松找到它。
  • 顺便说一句:最好使用$script: 变量,因为$global:会话-全局的,即它们在脚本退出后仍然存在。

标签: powershell ping windows-forms-designer


【解决方案1】:

我添加了一些 cmets 来帮助你理解思考过程。

Add-Type -AssemblyName System.Windows.Forms, System.Drawing

# Set a reference hashtable where you can store the Job's object
$jobRef = @{ Job = '' }

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Clevagroup Pinger'
$form.Size = New-Object System.Drawing.Size(350,150)
$form.StartPosition = 'CenterScreen'

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Ping"
$Button.Add_Click({

    # Save the Job on the reference hashtable
    $jobRef.Job = Start-Job -ScriptBlock {
        Ping 8.8.8.8 -t
    }

    # Disable the Ping Button
    $this.Enabled = $false

    # Enable the Stop Button
    $button1.Enabled = $true
})
$Form.Controls.Add($Button)

$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(195,35)
$Button1.Size = New-Object System.Drawing.Size(120,23)
$Button1.Text = "Stop"

# This button should be disabled by Default an only become 
# Enabled after 'Ping' button is Clicked
$Button1.Enabled = $false

$Button1.Add_Click({
    
    # Stop the Job
    Stop-Job $jobRef.Job

    # Receive the Job's result and store them in a Txt file
    Receive-Job $jobRef.Job | Out-File $env:userprofile\desktop\PingResults.txt

    # Remove the Job
    Remove-Job $jobRef.Job

    # Enable the Ping Button
    $button.Enabled = $true

    # Disable this Button
    $this.Enabled = $false
})

$form.Controls.Add($Button1)
$form.Add_Shown({
    $this.Activate()
    $Button.Select()
})
$form.ShowDialog()

【讨论】:

  • 干得好;使用哈希表而不是脚本级变量的有趣方法。它在这里并没有节省多少,但Receive-Job -Wait -AutoRemoveJob $jobRef.Job 可用于接收作业的输出并在单个操作中将其删除。
  • 谢谢@mklement0,好久没说话了 :) 从this answer 得到了hashtable 的解决方法,我个人认为比script 作用域变量更好的方法我可能错了。跨度>
  • :) 它绝对是$script: 变量引用的有效替代方案,并且可以说在概念上更简洁,但我会说这是一个品味问题。 (This answer 解释了为什么这两种方法都是必要的。)
猜你喜欢
  • 1970-01-01
  • 2011-05-28
  • 2013-10-22
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多