2016-07-26 8 views
0

Ich versuche, einen Thread zu erstellen, die eine gemeinsame Variable (zwischen Hauptsitzung und dem Thread) plus geben den Faden Fähigkeit zur Nutzung außerhalb Funktion vom HauptcodePowershell: Runspace/Pool Wie übergebe ich eine Funktion und eine gemeinsame Variable?

Ich habe es geschafft verwenden die Funktion zu übergeben der Thread zu verwenden und ich habe es geschafft, Read-Only-Variable übergeben. Mein Problem ist, dass, wenn ich den Wert der Variablen innerhalb des Threads ändern und dann versuche ich es aus der Hauptsitzung zu lesen - ich kann die Änderung des Wertes nicht sehen, daher ist es nicht geteilt.

Wie mache ich das? Mein Ziel ist es, einen Thread am Ende zu haben.

dies ist mein Code:

$x = [Hashtable]::Synchronized(@{}) 

$global:yo = "START" 

Function ConvertTo-Hex { 
    #Write-Output "Function Ran" 
    write-host "hi" 
    $x.host.ui.WriteVerboseLine("===========") 
    write-host $yo 
    $yo="TEST" 
    write-host $yo 
} 
#endregion 

     $rs = [RunspaceFactory]::CreateRunspace() 
     $rs.ApartmentState,$rs.ThreadOptions = "STA","ReUseThread" 
     $rs.Open() 
     $rs.SessionStateProxy.SetVariable("x",$x) 

ls 
# create an array and add it to session state 
$arrayList = New-Object System.Collections.ArrayList 
$arrayList.AddRange(('a','b','c','d','e')) 
$x.host = $host 
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault() 
$sessionstate.Variables.Add((New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry('arrayList', $arrayList, $null))) 
$sessionstate.Variables.Add((New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry('x', $x, $null))) 
#$sessionstate.Variables.Add((New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry('yo', $yo, $true))) 
$sessionstate.Commands.Add((New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'ConvertTo-Hex', (Get-Content Function:\ConvertTo-Hex -ErrorAction Stop))) 

$runspacepool = [runspacefactory]::CreateRunspacePool(1, 2, $sessionstate, $Host) 
$runspacepool.Open() 

$ps1 = [powershell]::Create() 
$ps1.RunspacePool = $runspacepool 

$ps1.AddScript({ 
    for ($i = 1; $i -le 15; $i++) 
    { 
     $letter = Get-Random -InputObject (97..122) | % {[char]$_} # a random lowercase letter 
     $null = $arrayList.Add($letter) 
     start-sleep -s 1 
    } 
}) 
$ps1.AddParameter(@($yo)) 

# on the first thread start a process that adds values to $arrayList every second 
$handle1 = $ps1.BeginInvoke() 

# now on the second thread, output the value of $arrayList every 1.5 seconds 
$ps2 = [powershell]::Create() 
$ps2.RunspacePool = $runspacepool 

$ps2.AddScript({ 
    Write-Host "ArrayList contents is " 
    foreach ($i in $arrayList) 
    { 
     Write-Host $i -NoNewline 
     Write-Host " " -NoNewline 
    } 
    Write-Host "" 
    $yo = "BAH" 
    ConvertTo-Hex 
}) 
$ps2.AddParameter(@($yo)) 


1..10 | % { 
    $handle2 = $ps2.BeginInvoke() 
    if ($handle2.AsyncWaitHandle.WaitOne()) 
    { 
     $ps2.EndInvoke($handle2) 
    } 
    start-sleep -s 1.5 
    write-host "====================" + $yo 
} 

Antwort

-3
$global:yo = "test" 

Sie müssen $global innerhalb der Funktion zu schreiben.

+0

Es funktioniert nicht .... –

Verwandte Themen