2015-09-07 34 views
0

我可以開始工作,但是如何在獲取信息時如何繼續使用我的程序?凍結程序直到結果完成。Start-Job Receive-Job在獲取結果時掛起/凍結 - PrimalForm CE GUI

這裏是PrimalForms CE版本生成的腳本,我的代碼在button1_OnClick

最終結果我想讓腳本運行來檢查鎖定的AD用戶(但那真的可以是任何東西),然後在運行時我想查看先前的結果並在需要時解鎖用戶...

#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 

回答

0

我不能談論PrimalForm但在PowerShell中工作室有一個在部分Toolbox > Control Sets稱爲Button - Start Job單獨的按鈕:

enter image description here

當使用這種典型提供一個額外功能的按鈕,稱爲Add-JobTracker,用於跟蹤作業進度並確保作業在其自己的PowerShell會話中在後臺運行。所以它不會凍結界面,甚至會更新進度條。

更多關於SAPIEN頁面PowerShell Studio: Creating responsive forms的信息。

事實上,你正在做一個循環來檢查每一次工作的狀態是爲什麼界面凍結。它在每一次停止時都會停下來,只有在完成任務後才能再次釋放。

也許如果你更新PrimalForms到最新版本,你將有這個功能。