I need help on Powershell GUI interface. On long last processes/loops GUI is freezing and I want to avoid that. I want to refresh GUI (richtextbox value) on backgroud or from different runspace.
I do not think that background job is a solution. Maybe a hastable and different runspace.
He is my code about this issue. Can anyone give me a better example or edit my code?
Copy code and save as .ps1
********** CODE ***************
#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: 2a715e2e-26db-488f-ab65-742aaad00d98
# Source File: GUIrefresh.psf
#------------------------------------------------------------------------
#region File Recovery Data (DO NOT MODIFY)
<#RecoveryData:
UQoAAB+LCAAAAAAABAC9Vl1v2jAUfZ+0/2DlOQJCPigSRCrZOlXrtqrQbm/ISW7Aw7GR7bRkv37O
B4yWFtKuoEhRjI997rnnxGFwAxG/B5F/wgoj/SAJZ0Oja/gfPyA0+CHIjDBMLwiF7zgF/8vtpYBE
gJy3ljIZtHcA1bLwN0QKqXwJQ2OcSwVp6ydhMX+QrQsu0upuouemTHRXV+G0OsVloiCjKhMwZJAp
gamJrrOQkugr5BO+ADYMez3sRq5n9W0HOmd9AzFdytBI9H6WgaI5obHQOCPgTAlOZaVOF3ot+BKE
yusFASXA1Jj8AcN3PM9EjucO2mvQC4sK2YZfch3ETmClDL/QuQv9fK+5a9wVx3G957R4HrTL2TX0
cH9HmVKcHb3DYUmzt8e7TbjiEVaa3vCtrolsq7fTi5eaXNM1wFce9lwTde0G8AkOL1kMK11SE3Tp
YvNibiXcEZlhOlY5hRGOFgGnXBj+RGTw3PrtKOhMRosN27QcPolDu8pD83TckGheiBjx1dEjIjSX
0lwhX/1XTqxu45hsUzbOiuNokm6/86q0NEKXadnMHrC7QAdzzGYQPxYy3ZrZ4//jwbmUkGqbQK6x
9S+5X9v+DTM8g1Tv1jrPFE/Ljv+z325qv22FiX3meji2PQdsfWxumJ4ypzLigpLwHUK2h6XSdwqO
d391DjMWX+vTEAn8QNjsLVwdO3GTXmJZsdvBNj7M9Sulp9FEhH5BuMjHIO5JBG+y7NXqAi7gJPJq
VfqY0dKOoG0zrE6VQXv7L6T/F10qjVRRCgAA#>
#endregion
<#
.NOTES
--------------------------------------------------------------------------------
Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.82
Generated on: 23.3.2015 14:19
Generated by: Administrator
Organization: W12
--------------------------------------------------------------------------------
.DESCRIPTION
GUI script generated by PowerShell Studio 2015
#>
#----------------------------------------------
#region Application Functions
#----------------------------------------------
#endregion Application Functions
#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-GUIrefresh_psf {
#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$button1 = New-Object 'System.Windows.Forms.Button'
$richtextbox1 = New-Object 'System.Windows.Forms.RichTextBox'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
# User Generated Script
#----------------------------------------------
$form1_Load={
}
$richtextbox1_TextChanged = {
$richtextbox1.Update()
}
$button1_Click = {
# Variables
$richtextbox1.Text = @()
# Map an existing hash table to a syncronized variable for use between threads
$ADHashTable = [HashTable]::Synchronized(@{ })
$ADHashTable.tmptxt = "Alfa"
# First value
$tmp = "First line" + "`n"
$richtextbox1.AppendText($tmp)
# Create a new RunSpace
$ADRunSpace = [RunSpaceFactory]::CreateRunSpace()
$ADRunSpace.ApartmentState = "STA"
$ADRunSpace.ThreadOptions = "ReuseThread"
$ADRunSpace.Open()
# Set Synchronized hash table variable
$ADRunSpace.SessionStateProxy.setVariable("ADHashTable", $ADHashTable)
# Custom code
$ADPowerShell = [PowerShell]::Create()
$ADPowerShell.Runspace = $ADRunSpace
$handle = $ADPowerShell.AddScript({
# Set simple value
$ADHashTable.tmptxt = "Beta" + "`n"
# Core code is here, this may take > 10 minutes.
# Update GUI (richtextbox) without freezing it.
# Import new values to GUI richtextbox.
<#
# This is simple example
$ADUsers = Get-ADUser -Filter * -Properties *
$ADUsers | % {
# Append user displayName to richtextbox
# Is is possible to user $ADHashTable.tmptxt variable
}
#>
}).BeginInvoke()
# Wait for the handle job to finish
while (-Not $handle.IsCompleted) {
Start-Sleep -Milliseconds 100
}
# Update simple value
$richtextbox1.AppendText($ADHashTable.tmptxt)
# Update last value
$richtextbox1.AppendText("All done")
# Close the session and dispose of PowerShell object
$ADPowerShell.EndInvoke($handle)
$ADRunSpace.Dispose()
}
# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$button1.remove_Click($button1_Click)
$richtextbox1.remove_TextChanged($richtextbox1_TextChanged)
$form1.remove_Load($form1_Load)
$form1.remove_Load($Form_StateCorrection_Load)
$form1.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($button1)
$form1.Controls.Add($richtextbox1)
$form1.ClientSize = '466, 465'
$form1.Name = "form1"
$form1.Text = "Form"
$form1.add_Load($form1_Load)
#
# button1
#
$button1.Location = '12, 317'
$button1.Name = "button1"
$button1.Size = '75, 23'
$button1.TabIndex = 1
$button1.Text = "button1"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_Click)
#
# richtextbox1
#
$richtextbox1.Location = '12, 12'
$richtextbox1.Name = "richtextbox1"
$richtextbox1.Size = '442, 290'
$richtextbox1.TabIndex = 0
$richtextbox1.Text = ""
$richtextbox1.add_TextChanged($richtextbox1_TextChanged)
$form1.ResumeLayout()
#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($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()
} #End Function
#Call the form
Call-GUIrefresh_psf | Out-Null