Solved

How To Update GUI From Another PowerShell Job

Posted on 2015-01-09
3
359 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
A procedure for exporting installed hotfix details of remote computers using powershell
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now