PaulHigin / PSThreadJob

A PowerShell module for running concurrent jobs based on threads rather than processes
MIT License
180 stars 18 forks source link

Add support for Using scope modifier #12

Closed PaulHigin closed 6 years ago

PaulHigin commented 6 years ago

This PR adds support for the Using keyword withing script blocks or script files supplied to Start-ThreadJob.

e.g.,
$var1 = "Hello"
$var2 = "There"
$job = Start-ThreadJob -ScriptBlock { "$using:var1 $using:var2" }

PowerShell handles Using keyword differently depending on PowerShell version and this change takes that into account.

This Using keyword support is only for variable sub expressions.

$using:var1

It does not support index sub expressions.

$using:array1[3]

I also updated ThreadJob class to derive from Job2 so that it can correctly handle terminating errors (Job class hides the terminating exception). This meant I had to implement a JobSourceAdapter to work with JobManager.

$job = Start-ThreadJob -ScriptBlock { throw "Error!" }
$job | Wait-Job | Receive-Job
Error!
+ CategoryInfo          : InvalidResult: (:) [], RuntimeException
+ FullyQualifiedErrorId : JobStateFailed
rjmholt commented 6 years ago

In my mind this is the "using scope modifier" rather than the "using keyword", the latter being for static module imports.

PaulHigin commented 6 years ago

Agreed. Changed the title.