0

我的 PowerShell 脚本中有三个组件。我为每个使用一个运行空间:核心、GUI 和计时器。

Core 用于执行某些操作,GUI 用于显示 core 中的操作是否已完成,并且 core 中的一项操作需要计时器。

我不确定这是一个干净/好的解决方案......因为我需要用 write-host 写一些信息并在这些运行空间之间交换一些信息。如果没有,你能告诉我更好的方法吗?

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()
}

编辑 :

图形用户界面线程:

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

定时器线程

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
4

0 回答 0