Prevent windows form from freeze

by Jun 28, 2011

Hello

I have a  windows form.

I call a ps1 file from the windows form. The ps1 file searches the whole registry and replaces paths. This ps1 runs about 3 minutes.

I want to post the result of the ps1 file in a richtextbox.

Until here, theres no problem.

But if I want to move the window, the window doesn't respond anymore.

I've tried to call the ps1 file threaded, but this doesn't work either.

I know that the form freezes because i have a wait-job $job in my code.

There's another problem, I can not update the richtextbox from a thread.

 

My code (is not very cleant at moment):

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Tools"
$objForm.Size = New-Object System.Drawing.Size(600,400)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown(
{
    if ($_.KeyCode -eq "F2")
    {       
        $ie = new-object -com "InternetExplorer.Application"
        $ie.navigate("http://www.hsr.ch/")
    }
})

$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Location = New-Object System.Drawing.Size(3,280)
$ProgressBar.Size = New-Object System.Drawing.Size(577,20)
$ProgressBar.Minimum = 0
$ProgressBar.Maximum = 100
$objForm.Controls.Add($ProgressBar)

#Checkbox für HKCU modifizieren
$HKUCheckBox = New-Object System.Windows.Forms.CheckBox
$HKUCheckBox.Location = New-Object System.Drawing.Size(3,3)
$HKUCheckBox.Size = New-Object System.Drawing.Size(15,15)
$HKUCheckBox.Checked = $True
$objForm.Controls.Add($HKUCheckBox)
#Label für HKCU modifizieren
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(18,3)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Benutzer registry anpassen"
$objForm.Controls.Add($objLabel)

#Checkbox für Desktop Shortcuts modifizieren
$DSCheckBox = New-Object System.Windows.Forms.CheckBox
$DSCheckBox.Location = New-Object System.Drawing.Size(3,23)
$DSCheckBox.Size = New-Object System.Drawing.Size(15,15)
$DSCheckBox.Checked = $True
$objForm.Controls.Add($DSCheckBox)
#Label für Desktop Shortcuts modifizieren
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(18,23)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Desktopverknüpfungen anpassen"
$objForm.Controls.Add($objLabel)
#Checkbox für Desktop Shortcuts modifizieren
$NLCheckBox = New-Object System.Windows.Forms.CheckBox
$NLCheckBox.Location = New-Object System.Drawing.Size(3,43)
$NLCheckBox.Size = New-Object System.Drawing.Size(15,15)
$NLCheckBox.Checked = $True
$objForm.Controls.Add($NLCheckBox)
#Label für Desktop Shortcuts modifizieren
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(18,43)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Networklocation erstellen"
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.RichTextBox
$objTextBox.Multiline = $True
$objTextBox.Location = New-Object System.Drawing.Size(3,200)
$objTextBox.Size = New-Object System.Drawing.Size(577,80)

$objForm.Controls.Add($objTextBox)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,320)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click(
{
    $ProgressBar.Value = 0
    $objTextBox.Text = ''
    $objTextBox.SelectionStart = $objTextBox.Text.Length;
    $objTextBox.ScrollToCaret()
    $steps = 0
    if($NLCheckBox.Checked)
    {
        $steps++
    }
    if($DSCheckBox.Checked)
    {
        $steps++
    }
    if($HKUCheckBox.Checked)
    {
        $steps++
    }
   
    #disables the two buttons
    $OKButton.Enabled = $False
    $CancelButton.Enabled = $False

    if($NLCheckBox.Checked)
    {   
        $objTextBox.Lines += "`r`nNetworklocations werden erstellt."
        $objTextBox.Lines += "———————————"
        $objTextBox.SelectionStart = $objTextBox.Text.Length;
        $objTextBox.ScrollToCaret()
        $objForm.Update()
           
        $ProgressBar.Value = $ProgressBar.Value + ($ProgressBar.Maximum/$steps)
        &(($MyInvocation.MyCommand.Definition -replace $MyInvocation.MyCommand.Name,"")+'Create_NetworkLocation.ps1') $objTextBox
    }
    if($DSCheckBox.Checked)
    {
        $ProgressBar.Value = $ProgressBar.Value + ($ProgressBar.Maximum/$steps)
    }
    if($HKUCheckBox.Checked)
    {
        $objTextBox.Lines += "`r`nRegistry wird angepasst."
        $objTextBox.Lines += "————————"
        $objTextBox.SelectionStart = $objTextBox.Text.Length;
        $objTextBox.ScrollToCaret()
        $objForm.Update()
        $ProgressBar.Value = $ProgressBar.Value + ($ProgressBar.Maximum/$steps)
        $psFile = (($MyInvocation.MyCommand.Definition -replace $MyInvocation.MyCommand.Name,"")+'test.ps1')
       
        $job  = start-job { Start-Sleep -Seconds 10 }
        Wait-Job $job
    }
   
   
    $ProgressBar.Value = $ProgressBar.Maximum
   
    $OKButton.Enabled = $True
    $CancelButton.Enabled = $True
}
)
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,320)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

#$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()