1

I'm working on Powershell GUI which show to an user, each step for establish a connection to a VPN. So when he start the script, he will see all's steps, with the current step in bold and the last completed step in light gray. A Webbrowser is here to show additionnal info by loading html file related to the current step.

I don't understand how I must load my GUI on one hand, and do my backgrounds jobs on the other hand. In this exemple, it's working because I have created a button event for change the html :

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$SyncHash = [hashtable]::Synchronized(@{})

$Main = New-Object Windows.Forms.Form
$Main.Width = 768
$Main.Height = 576

$button_1 = New-Object System.Windows.Forms.Button
$button_1.Text = "1"
$button_1.Size = New-Object System.Drawing.Size(20,20)
$button_1.Location = New-Object System.Drawing.Size(20,10)
$Main.Controls.Add($button_1)

$SyncHash.infoBrowser = new-object Windows.Forms.WebBrowser
$SyncHash.infoBrowser.Size = new-object System.Drawing.Size(710,200)
$SyncHash.infoBrowser.Location = new-object System.Drawing.Size(10,290)
$SyncHash.infoBrowser.ScrollBarsEnabled = $true
$Main.Controls.Add($SyncHash.infoBrowser)

$htmlFile = "h:\csv\Test.html"
$html = get-content $htmlFile
$SyncHash.infoBrowser.DocumentText = $html 

$button_1.Add_Click( 
{ 
    $htmlFile = "h:\csv\underLAN.html"
    $html = get-content $htmlFile
    write-host $html
    $SyncHash.infoBrowser.DocumentText = $html
}) 

[Windows.Forms.Application]::Run($Main)

But, if try to use a job, nothings happens.

...
$underLAN={
    $htmlFile = "h:\csv\underLAN.html"
    $html = get-content $htmlFile
    write-host $html
    $SyncHash.infoBrowser.DocumentText = $html
}

[Windows.Forms.Application]::Run($Main)
Start-Job -Name test -ScriptBlock $underLAN  | Receive-Job 
mrplume
  • 183
  • 1
  • 3
  • 18
  • Register an event for OnLoad or something like that for the form that starts your job when it loads. If you have steps you could create a series of jobs, and update your GUI in between jobs. If you want something truly multi-threaded for something like this then it's going to be much more complicated. Why do you want it to run as a job? – TheMadTechnician May 04 '16 at 17:35
  • For a PowerShell example of a GUI with a multi-treathed background worker see: [Write PowerShell Output (as it happens) to WPF UI Control](http://stackoverflow.com/questions/23142137/write-powershell-output-as-it-happens-to-wpf-ui-control/36716964#36716964) – iRon May 18 '16 at 08:48
  • Thanks you. This link [link](https://learn-powershell.net/2012/09/13/powershell-and-wpf-introduction-and-building-your-first-window/) give also a good advice. I took this way, but I"m using winform because my ui is very simple – mrplume May 18 '16 at 09:59

0 Answers0