PowerShell一次显示一个数组项

时间:2017-05-05 03:56:12

标签: arrays powershell richtextbox

我有一个数组,想要一次显示一个项目。每个项目也应以不同的时间间隔显示。我想我应该使用Start-Sleep -s 5。 到目前为止,这是我的代码。

function GenerateForm {

$a=
"Downloading Files", 
"Setting up VPN Connection", 
"Getting computer information", 
"Install Complete", 
"Cluster Flux Technologies", 
"You are Free to close this application" 



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

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
$pictureBox1 = New-Object System.Windows.Forms.PictureBox
$btn1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$btn1_OnClick= {
        $richTextBox1.Text = $a | Out-String
    }
    #----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,212,208,200)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 658
$System_Drawing_Size.Width = 1072
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Users\502706436\Desktop\tight vnc\cluster.ico')
$form1.Name = "form1"
$form1.Text = "Cluster Flux Technologies: VNC Installer"

$richTextBox1.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox1.Enabled = $False
$richTextBox1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,0)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 566
$System_Drawing_Point.Y = 570
$richTextBox1.Location = $System_Drawing_Point
$richTextBox1.Name = "richTextBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 39
$System_Drawing_Size.Width = 348
$richTextBox1.Size = $System_Drawing_Size
$richTextBox1.TabIndex = 2
$richTextBox1.Text = ""

$form1.Controls.Add($richTextBox1)

$pictureBox1.BackgroundImage = [System.Drawing.Image]::FromFile('C:\Users\502706436\Desktop\tight vnc\cluster2 logo.png')
$pictureBox1.BackgroundImageLayout = 2
$pictureBox1.DataBindings.DefaultDataSourceUpdateMode = 0



$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 41
$System_Drawing_Point.Y = 35
$pictureBox1.Location = $System_Drawing_Point
$pictureBox1.Name = "pictureBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 454
$System_Drawing_Size.Width = 949
$pictureBox1.Size = $System_Drawing_Size
$pictureBox1.TabIndex = 1
$pictureBox1.TabStop = $False

$form1.Controls.Add($pictureBox1)


$btn1.DataBindings.DefaultDataSourceUpdateMode = 0
$btn1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 71
$System_Drawing_Point.Y = 570
$btn1.Location = $System_Drawing_Point
$btn1.Name = "btn1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 39
$System_Drawing_Size.Width = 292
$btn1.Size = $System_Drawing_Size
$btn1.TabIndex = 0
$btn1.TabStop = $False
$btn1.Text = "Click to install connection"
$btn1.UseVisualStyleBackColor = $True
$btn1.add_Click($btn1_OnClick)

$form1.Controls.Add($btn1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState

#Show the Form
$form1.ShowDialog()

}   #End Function

#Call the Function
GenerateForm

如果有人知道或有关于如何实现这一目标的建议,我将不胜感激。 感谢

1 个答案:

答案 0 :(得分:0)

要更新文本,您可以使用:

$btn1_OnClick= {
      $a | %{  $richTextBox1.Text = $_ | Out-String;Start-Sleep -s 5}
    }

但上述简单方法会在上面的块运行时冻结ui。您可以尝试以下的runpace方法,这将确保ui在更新期间响应:

$btn1_OnClick= {

$Runspace = [runspacefactory]::CreateRunspace()

$PowerShell = [powershell]::Create()

$PowerShell.runspace = $Runspace

$Runspace.Open()

[void]$PowerShell.AddScript({
param($richTextBox1)

$a=
"Downloading Files", 
"Setting up VPN Connection", 
"Getting computer information", 
"Install Complete", 
"Cluster Flux Technologies", 
"You are Free to close this application" 

 $a | %{  $richTextBox1.Text = $_ | Out-String;Start-Sleep -s 5;}

}).addargument($richTextBox1)

$AsyncObject = $PowerShell.BeginInvoke()

    }