Winforms 开始作业\接收作业挂起\在获取结果时冻结-PrimalForm CE GUI

Winforms 开始作业\接收作业挂起\在获取结果时冻结-PrimalForm CE GUI,winforms,powershell,powershell-4.0,Winforms,Powershell,Powershell 4.0,我可以开始工作,但我如何在程序获取信息时继续使用它,如果我执行“while”循环,它只会冻结程序,直到结果完成 这是由PrimalForms CE edition生成的脚本,我的代码在button1_OnClick中 最终结果我想让脚本运行,以检查锁定的广告用户,但这真的可以是任何东西,然后在运行时,我想审查以前的结果,并解锁用户,如果需要 #Generated Form Function function GenerateForm { ###########################

我可以开始工作,但我如何在程序获取信息时继续使用它,如果我执行“while”循环,它只会冻结程序,直到结果完成

这是由PrimalForms CE edition生成的脚本,我的代码在button1_OnClick中

最终结果我想让脚本运行,以检查锁定的广告用户,但这真的可以是任何东西,然后在运行时,我想审查以前的结果,并解锁用户,如果需要

#Generated Form Function
function GenerateForm {
  ########################################################################
  # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition)      v1.0.10.0
  # Generated On: 7/09/2015 2:24 PM
  # Generated By: stojanp2
  ########################################################################

  #region Import the Assemblies
  [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  #endregion

  #region Generated Form Objects
  $form1 = New-Object System.Windows.Forms.Form
  $button3 = New-Object System.Windows.Forms.Button
  $button2 = New-Object System.Windows.Forms.Button
  $listView1 = New-Object System.Windows.Forms.ListView
  $button1 = New-Object System.Windows.Forms.Button
  $Users = New-Object System.Windows.Forms.ColumnHeader
  $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  #endregion Generated Form Objects

  #----------------------------------------------
  #Generated Event Script Blocks
  #----------------------------------------------
  #Provide Custom Code for events specified in PrimalForms.
  $button3_OnClick =
  {
    #TODO: Place custom script here
  }

  $button1_OnClick =
  {
    Start-Job -Name FindUsers -ScriptBlock { Search-ADAccount -LockedOut | Select-Object * }#end Start-Job

    Do {
       Start-Sleep -Seconds 1
       write-host "waiting" # so i can see its doing something in the console
    } Until (@(Get-Job -Name FindUsers).State -eq "Completed")

    Write-Host "done"

    $us = Receive-Job -Name FindUsers
    ForEach ($u IN $us){
      $listView1.Items.Add($u.sAMAccountName)
    }

    Get-Job -Name FindUsers | Remove-Job -Force
  }

  $button2_OnClick =
  {
    #TODO: Place custom script here
  }

  $handler_listView1_SelectedIndexChanged =
  {
    #TODO: Place custom script here
  }

  $OnLoadForm_StateCorrection =
  { #Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
  }

  #----------------------------------------------
  #region Generated Form Code
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 469
  $System_Drawing_Size.Width = 271
  $form1.ClientSize = $System_Drawing_Size
  $form1.DataBindings.DefaultDataSourceUpdateMode = 0
  $form1.Name = "form1"
  $form1.Text = "Primal Form"

  $button3.DataBindings.DefaultDataSourceUpdateMode = 0

  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 142
  $System_Drawing_Point.Y = 418
  $button3.Location = $System_Drawing_Point
  $button3.Name = "button3"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 23
  $System_Drawing_Size.Width = 117
  $button3.Size = $System_Drawing_Size
  $button3.TabIndex = 3
  $button3.Text = "button3"
  $button3.UseVisualStyleBackColor = $True
  $button3.add_Click($button3_OnClick)

  $form1.Controls.Add($button3)

  $button2.DataBindings.DefaultDataSourceUpdateMode = 0

  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 418
  $button2.Location = $System_Drawing_Point
  $button2.Name 

  = "button2"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 23
  $System_Drawing_Size.Width = 117
  $button2.Size = $System_Drawing_Size
  $button2.TabIndex = 2
  $button2.Text = "button2"
  $button2.UseVisualStyleBackColor = $True
  $button2.add_Click($button2_OnClick)

  $form1.Controls.Add($button2)

  $listView1.Columns.Add($Users)|Out-Null
  $listView1.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 73
  $listView1.Location = $System_Drawing_Point
  $listView1.Name = "listView1"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 339
  $System_Drawing_Size.Width = 247
  $listView1.Size = $System_Drawing_Size
  $listView1.TabIndex = 1
  $listView1.UseCompatibleStateImageBehavior = $False
  $listView1.View = 1
  $listView1.add_SelectedIndexChanged($handler_listView1_SelectedIndexChanged)

  $form1.Controls.Add($listView1)

  $button1.DataBindings.DefaultDataSourceUpdateMode = 0

  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 12
  $button1.Location = $System_Drawing_Point
  $button1.Name = "button1"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 55
  $System_Drawing_Size.Width = 247
  $button1.Size = $System_Drawing_Size
  $button1.TabIndex = 0
  $button1.Text = "button1"
  $button1.UseVisualStyleBackColor = $True
  $button1.add_Click($button1_OnClick)

  $form1.Controls.Add($button1)

  $Users.Name = "Users"
  $Users.Text = "User Names"
  $Users.Width = 116

  #endregion Generated Form Code

  #Save the initial state of the form
  $InitialFormWindowState = $form1.WindowState
  #Init the OnLoad event to correct the initial state of the form
  $form1.add_Load($OnLoadForm_StateCorrection)
  #Show the Form
  $form1.ShowDialog() | Out-Null
} #End Function

#Call the Function
GenerateForm

我不能谈论PrimalForm,但在PowerShell Studio中,工具箱>控制集部分中有一个单独的按钮,名为“按钮启动作业”:

使用此类按钮时,会提供一个名为Add JobTracker的额外功能,用于跟踪作业进度,并确保作业在其自己的PowerShell会话中在后台运行。因此,它不会冻结界面,如果愿意,甚至会更新进度条

更多信息请访问SAPIEN页面

每次作业的状态都会执行循环检查,这就是界面冻结的原因。它每次都会在该点停止,只有在工作完全完成时才会再次释放

如果您将PrimalForms更新到最新版本,您可能会使用此函数