0

I have three components in my PowerShell script. I'm using a runspace for each : core, GUI and timer.

Core is for executing some actions, GUI for showing if action in core is completed and timer is needed for one action in core.

I'm not sure It's a clean/good solution... because I need to write some information with write-host and exchange some info between theses runspaces. Could you tell me a better way if not ?

Function StartRunSpaceForGui {
    #-RunSpace for GUI---------------
    $global:RunSpaceForGui=[runspacefactory]::CreateRunspace()
    $global:RunSpaceForGui.ApartmentState="STA"
    $global:RunSpaceForGui.ThreadOptions="ReuseThread"
    $global:RunSpaceForGui.Open()

    $global:HashForGui=[hashtable]::Synchronized(@{})
    $global:RunSpaceForGui.SessionStateProxy.SetVariable("HashForGui",$HashForGui)

    $global:ScriptForGui=[PowerShell]::Create().AddScript("$global:ProductPath\PS\Gui.ps1")
    $global:ScriptForGui.RunSpace=$RunSpaceForGui
    $global:HandleForGui=$ScriptForGui.BeginInvoke()
}

Function StartRunSpaceForEngine {
    #-RunSpace for Engine---------------------#
    $global:RunSpaceForEngine=[runspacefactory]::CreateRunspace()
    $global:RunSpaceForEngine.Open()

    $global:ScriptForEngine=[PowerShell]::Create().AddScript("$global:ProductPath\PS\Engine.ps1")
    $global:ScriptForEngine.RunSpace=$RunSpaceForEngine
    $global:HandleForEngine=$ScriptForEngine.BeginInvoke()
}

Function StartRunSpaceForTimer {
    #-RunSpace for timer
    $global:RunSpaceForTimer=[runspacefactory]::CreateRunspace()
    $global:RunSpaceForTimer.Open()

    $global:ScriptForTimer=[PowerShell]::Create().AddScript("$global:ProductPath\PS\Timer.ps1")
    $global:ScriptForTimer.RunSpace=$RunSpaceForTimer
    $global:HandleForTimer=$ScriptForTimer.BeginInvoke()
}

EDIT :

GUI thread :

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#--Main Form Creation.
$HashForGui.MainForm=New-Object System.Windows.Forms.Form
$HashForGui.MainForm.Size=New-Object System.Drawing.Size($Width,$Height)
$HashForGui.MainForm.StartPosition="CenterScreen"
$HashForGui.MainForm.FormBorderStyle=[System.Windows.Forms.FormBorderStyle]::FixedDialog
$HashForGui.MainForm.MaximizeBox=$False
$HashForGui.MainForm.MinimizeBox=$False
$HashForGui.MainForm.ShowInTaskbar=$True
$HashForGui.MainForm.WindowState="Normal"
$HashForGui.MainForm.Text=$ProductName
$Icon=New-Object system.drawing.icon($ProductIconFilePath)
$HashForGui.MainForm.Icon=$Icon

#---WebBrowser Creation.
$HashForGui.MainWebBrowser=New-Object System.Windows.Forms.WebBrowser
$HashForGui.MainWebBrowser.Size=New-Object System.Drawing.Size($Width,$Height)
$HashForGui.MainWebBrowser.Location=New-Object System.Drawing.Size(0,0)
$HashForGui.MainWebBrowser.ScrollBarsEnabled=$True
$HashForGui.MainForm.Controls.Add($HashForGui.MainWebBrowser)  

#---WebBrowser info section load.
$HashForGui.MainWebBrowser.DocumentText=get-content $HtmlLoadingFilePath
$HashForGui.MainForm.ShowDialog()
exit

Timer thread

Remove-ItemProperty -Path "Registry::$ProductRegistryPath" -Name "Time Out" -Force -ErrorAction SilentlyContinue

[Int32]$Timer=0
do {
    start-sleep -s 1
    $Timer=$Timer+1
    if ($Debug -eq 1) {write-host -Foreground Blue "Timer : $Timer/$MaxTimer"}
} until ($Timer -ge $MaxTimer)

New-ItemProperty -Path "Registry::$ProductRegistryPath" -Name "Time Out" -PropertyType String -value "1" -Force
exit
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
mrplume
  • 183
  • 1
  • 3
  • 18

0 Answers0