0

I want create a queue of ffmpeg commands and then let N threads consume the queue launching an instance of ffmpeg with parameters:

here some code:

#looping on $items to prepare the params
foreach($input in $items){
    {...}

    #adding a param in queue
    $global:jobsQueue.Enqueue($ffmpegParam)
} 

#block code to be executed in different thread
$block = {
    Param($queue, $ffmpegDir)
    while($true){
        if($queue.TryDequeue($params)){
            $pinfo = New-Object System.Diagnostics.ProcessStartInfo($ffmpegDir, $params)
            $pinfoMap.$input.UseShellExecute = $false
            $pinfoMap.$input.CreateNoWindow = $true
            $p = New-Object System.Diagnostics.Process
            $p.StartInfo = $pinfo
            $p.Start()
            $p.WaitForExit()
        } else {break}
    }
}

#miserably failing to start the previous block code
for($i = 0; $i -lt 1; $i++){
    Start-Job -Name "process $i" -ScriptBlock $block -ArgumentList $global:jobsQueue,$ffmpegDir
}

A job is actually started but it does nothing and i don't get why. I read those pages but i wasn't able to come up with a solution:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job?view=powershell-5.1

How do I Start a job of a function i just defined?

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/b68c1c68-e0f0-47b7-ba9f-749d06621a2c/calling-a-function-using-startjob

Lamberto Basti
  • 478
  • 1
  • 6
  • 24

0 Answers0