Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How To Update GUI From Another PowerShell Job

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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