Param([string]$DataFolder,[string]$Background,[string]$OutputFolder,[switch]$DoMove)
#Unrelated code
Function EachZ{
Param([string]$ZFolder)
#Unrelated code
}
[System.Threading.Tasks.Parallel]::ForEach([System.IO.Directory]::GetDirectories($DataFolder),$EachZ)
Run it and error occurs: Multiple ambiguous overloads found for "ForEach" and the argument count: "2".
What's wrong here?
Perhaps @daxian-dbw could look the binder issue.
@Silver-Fang I assume you are trying to use the function EachZ as a delegate there?
The main problem (at least with binding) is that PowerShell's binder doesn't attempt to determine what type of delegate you want based on the contents of the script block provided. If there's one overload that takes a delegate (or the delegate isn't the ambigious part) it can figure it out, otherwise every single overload seems like it matches as far as the binder is concerned. You can get around this by casting:
[Threading.Tasks.Parallel]::ForEach(
[IO.Directory]::GetDirectories($PWD.Path),
[Action[string]]$function:EachZ)
That'll make it past binding, but it'll hit the bigger problem. You can't run a ScriptBlock on a thread without a default Runspace. It just throws. Even if you could, it would just marshal the invocation back to the thread it came from, making the API pointless.
In other words, you just really can't use Parallel.ForEach. Use Start-ThreadJob or similar instead.
Wonder if there's a way to make that work somehow 馃
Probably an awful lot of work to get some kind of usable output or other streams from the invocation though.
Yeah there's plenty of ways to make it technically functional. None of those ways really make sense over just using existing PowerShell-centric threading methods though.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.