Solved

How To Update GUI From Another PowerShell Job

Posted on 2015-01-09
3
429 Views
Last Modified: 2015-01-12
So, I have a form. I want a button click to do something without locking up the form. I've done this in C#: I create a background worker and use the BeginInvoke method to update GUI elements and be thread safe. How do I do it in PowerShell? Here is what I've got so far:

$mainform = New-Object System.Windows.Forms.Form
$mainform.Text = "Test Form"
$mainform.Size = New-Object System.Drawing.Size(500,500)
$mainform.StartPosition = "CenterScreen"
$mainform.AutoSize = $false
$mainform.MinimizeBox = $false
$mainform.MaximizeBox = $false
$mainform.SizeGripStyle = "Hide"
$mainform.WindowState = "Normal"
$mainform.FormBorderStyle = "Fixed3D"
$mainfont = New-Object System.Drawing.Font("Arial",12)
$mainform.Font = $font

$statustxtbx = New-Object System.Windows.Forms.TextBox
$statustxtbx.Size = New-Object System.Drawing.Size(400,300)
$statustxtbx.Top = 60
$statustxtbx.Name = "StatusTextBox"
$statustxtbx.Multiline = $true
$statustxtbx.ScrollBars = "Vertical"
$statustxtbx.WordWrap = $true
$mainform.Controls.Add($statustxtbx)

$addbtn = New-Object System.Windows.Forms.Button
$addbtn.Size = New-Object System.Drawing.Size(80,30)
$addbtn.Text = "Add"
$addbtn.Add_Click({
    Start-Job -Name AddClick -ScriptBlock {
        [System.Windows.Forms.Messagebox]::Show("Hi")
        $statustxtbx.BeginInvoke({$statustxtbx.Text = "Disabling controls..."})
        for ($i = 0;$i -lt $mainform.Controls.Count; $i++) {
            $c = $mainform.Controls[$i]
            if ($c.Name -notlike "StatusTextBox") {
                $c.BeginInvoke({$c.Enabled = $false})
            }
        }
        $statustxtbx.BeginInvoke({$statustxtbx.Text += "done.`r`n`r`nSleeping for 10 seconds..."})
        sleep -Seconds 10
        $mainform.BeginInvoke({$statustxtbx.Text += "done.`r`nEnabling controls..."})
        for ($i = 0;$i -lt $mainform.Controls.Count; $i++) {
            $c = $mainform.Controls[$i]
            if ($c.Name -notlike "StatusTextBox") {
                $mainform.BeginInvoke({$c.Enabled = $true})
            }
        }
        $statustxtbx.BeginInvoke({$statustxtbx.Text += "done."})
    }
    #$statustxtbx.Text += "Some Text `r`n"
})
$mainform.Controls.Add($addbtn)
$mainform.Add_Shown({$mainform.Activate()})
$mainform.ShowDialog()

Open in new window

0
Comment
Question by:byt3
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 40543009
Hi
You have used start-job, when  I think you need to be using runspaces which will allow you to share  varialbles between processes

An example of this can be found here  , http://learn-powershell.net/2013/04/19/sharing-variables-and-live-objects-between-powershell-runspaces/sharing-variables-and-live-objects-between-powershell-runspaces

if you detail you exact requirements i will try and help you
0
 
LVL 2

Author Comment

by:byt3
ID: 40544577
Thanks for the link. I will check it out and see how to use runspaces.

Ultimately this is a tool that a client will use to create public folders and mail enable them. That part I don't need assistance on. What I need help with is after the button is clicked and the public folder stuff is happening I need to the form to not lock up by using a background process somehow. During the process I want to update the text in the textbox as an output of what the tool is currently doing.
0
 
LVL 2

Author Comment

by:byt3
ID: 40546000
Runscpaces is exactly what I needed. I also need to change the code for invoking the control as well:

BeginInvoke( { <code> } )

Open in new window


To

BeginInvoke( [action]{ <code } )

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question