Private/Thread.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function StartThread([ScriptBlock]$scriptBlock, $arguments) { $runspace = [RunSpaceFactory]::CreateRunspace() $runspace.Open() $powershell = [PowerShell]::Create() $powershell.Runspace = $runspace $powershell.AddScript($scriptBlock.ToString()) | Out-Null $powershell.AddArgument($arguments) | Out-Null $asyncHandle = $powershell.BeginInvoke() $thread = [PSCustomObject]@{ Runspace = $runspace Powershell = $powershell AsyncHandle = $asyncHandle } $thread } function StopThread($thread) { $thread.Runspace.Dispose() $thread.Powershell.Dispose() } |