I have a little WPF Powershell GUI with a timer:
##############################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$sync = [hashtable]::Synchronized(@{})
##############################################
##############################################Start form
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,400)
$Form.Text = "Testor"
$Form.MaximizeBox = $false
############################################## Start functions
Function Netchk {
$wlanchk1 = netsh wlan show interfaces | Select-String '\sSSID'
if ($wlanchk1 -ne $null){$wlanchk = $wlanchk1 -replace ('\s','')
$wlanchk -replace 'SSID:','Connected to: '}else{$wlanchk = "No wlan connected"}
$outputBox.Text = $wlanchk
}
############################################## end functions
############################################## Start group box
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,10)
$groupBox.Autosize = $true
$groupBox.text = "Groupbox: "
$Form.Controls.Add($groupBox)
############################################## end group box
############################################## Start buttons
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Point(15,25)
$Button1.Size = New-Object System.Drawing.Size(200,30)
$Button1.Text = "Button1"
$groupBox.Controls.Add($Button1)
$Button1.Add_click({netchk})
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = new-object System.Drawing.Point(15,55)
$Button2.Size = New-Object System.Drawing.Size(200,30)
$Button2.Text = "Button2"
$groupBox.Controls.Add($Button2)
$Button2.Add_click({})
$Button3 = New-Object System.Windows.Forms.Button
$Button3.Location = new-object System.Drawing.Point(15,85)
$Button3.Size = New-Object System.Drawing.Size(200,30)
$Button3.Text = "Button3"
$groupBox.Controls.Add($Button3)
$Button3.Add_click({})
$Button4 = New-Object System.Windows.Forms.Button
$Button4.Location = new-object System.Drawing.Point(15,115)
$Button4.Size = New-Object System.Drawing.Size(200,30)
$Button4.Text = "Button4"
$groupBox.Controls.Add($Button4)
$Button4.Add_click({})
############################################## end buttons
############################################## Start text field
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,200)
$outputBox.Size = New-Object System.Drawing.Size(565,150)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$outputBox.Text = 0
$Form.Controls.Add($outputBox)
$Form.Controls.AddRange(@($sync.Textbox))
############################################## end text field
############################################## start label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object Drawing.Point (385, 30)
$label1.Width = 100
$label1.Height = 60
$label1.Text = 0
$label1.Font = New-Object System.Drawing.Font("Courier New",32,1,2,0)
############################################## end label
############################################## start timer
$timer1 = New-Object System.Windows.Forms.Timer
$timer1.Interval = 1000
$timer1.Enabled = $true
$time = 60
$script:StartTime = (Get-Date).AddSeconds($Time)
$timer1_OnTick = {
[TimeSpan]$span = $script:StartTime - (Get-Date)
$label1.Text = '{0:N0}' -f $span.TotalSeconds
if($span.TotalSeconds -le 0)
{
$timer1.Stop()
$timer1.enabled = $false
function1
$Form.Close()
stop-process -Id $PID
}
}
$timer1.add_tick($timer1_OnTick)
$Form.Controls.AddRange(@($sync.Timer))
##############################################
$sync.button1 = $button1
$sync.button2 = $button2
$sync.button3 = $button3
$sync.button4 = $button4
$sync.label1 = $label1
$sync.TextBox = $outputBox
$sync.Groupbox = $groupBox
$sync.Timer = $timer1
$Form.Controls.AddRange(@($sync.button1, $sync.button2, $sync.button3, $sync.button4, $sync.label1, $sync.TextBox, $sync.Groupbox ))
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
Because I'm new to powershell, I still cannot understand the runspaces launch method etc. How can I run the timer from its own runspace and how can I add functions to buttons? I need something {click} --> {open new runspace} --> {run function} while timer is still ticking in isolation. The netchk function is a simple task, what i want GUI to do.
I want to understand this =) Please, explain it to me.