0

As the title says, I have two processes I would like to run and wait until both are completed.

I'm trying to create a powershell function that will invoke a rest api as well as call an exe that will display a dialog.

function SaySpeechWithPrompt($message, $timeout, $promptTitle="") {
    $job1 = {
        param($messageArg)
        Invoke-RestMethod "http://localhost:9090/api/saySpeech?message=$messageArg" -Method Get
    }
    $job2 = {
        param($promptTitleArg, $messageArg, $timeoutArg)
        .\Dialog\Dialog.exe $promptTitleArg $messageArg 6 $timeoutArg
    }
    Start-Job -ScriptBlock $job1 -ArgumentList $message
    Start-Job -ScriptBlock $job2 -ArgumentList $promptTitle, $message, $timeout
    While (Get-Job -State "Running")
    {
        Start-Sleep 1
    }
}

This is what I have so far. Doesn't work well. Few problems

  1. The dialog is never displayed, seems like running this in a scriptblock is causing issues.
  2. I'm not sure that the above will call both processes and wait for both to finish. I.e is Get-Job just checking the last job? I need it to check both jobs.
  3. Is there a better way of doing this without sleeping in a loop?

I'm very new to powershell!

rukiman
  • 597
  • 10
  • 32
  • ok did some more debugging. seems this does wait correctly as I want for both processes to complete. However the issue is .\Dialog\Dialog.exe doesn't work inside the scriptblock, if you put it outside the scriptblock it works. I used the full path and that worked. What's going on here? I thought the working directory would be the same? – rukiman Jun 29 '20 at 07:00
  • Start-Job does not inherit the working directory. The default in windows is $HOME\Documents. – guiwhatsthat Jun 29 '20 at 07:44
  • 1
    Please change your question (or create a new one) with your new insights. – iRon Jun 29 '20 at 07:47
  • Like @guiwhatsthat said, `Start-job` does not inherit Working directory. But like you already do, you can pass in the working directory as an argument to the scriptblock job2. While sleep is a perfectly good way to wait, you could also do `Get-Job | Wait-Job`. If you expect any data return from the scriptblock, u can use `Get-Job | Wait-Job | receive-Job`. – Sid Jun 29 '20 at 07:58
  • i THINK that PoSh jobs run in a background, non-interactive session. that means any UI stuff would not show up since there is no console for it to show in. – Lee_Dailey Jun 29 '20 at 09:24
  • In short: _Up to PowerShell v6.x_, the working directory for `Start-Job` is the user's _Documents_ directory, not the _caller's_ current directory; changing to the caller's / a given directory requires an explicit `Set-Location` call as part of the script block (or via a separate script block passed to `-InitializationScript` ). In _PowerShell 7+_, the caller's working directory is now inherited by default, and a new `-WorkingDirectory` parameter allows specifying a directory explicitly. See [this answer](https://stackoverflow.com/a/53018311/45375) to the linked duplicate. – mklement0 Jun 08 '21 at 18:28
  • @Lee_Dailey, it's only _console_ output that doesn't show immediately (because PowerShell collects it behind the scenes, to be retrieved on demand with `Receive-Job`). By contrast, a _GUI_ window does show instantly. – mklement0 Jun 08 '21 at 18:44

0 Answers0