How do you wait on a Task Scheduler task to finish in a batch file or C#?

前端 未结 3 1130
日久生厌
日久生厌 2021-01-04 14:37

I am trying to write a batch file that does two things:

  1. First it launches an installer (install.exe) which installs a program (program.exe).
  2. Second it
相关标签:
3条回答
  • 2021-01-04 14:42

    You can also get rid of the hacky ping -n command by using timeout.

    Here's the answer of MC ND with timeout. The 1 in the sample stands for 1 second.

    :loop
    for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
        if "%%f"=="Running" (
            timeout /T 1 /NOBREAK > nul
            goto loop
        )
    )
    
    0 讨论(0)
  • 2021-01-04 15:03

    thank you! confirmed working on windows 10 cmd file. very useful way to create multiple aysnc tasks and sync them back up. For example, chkdsk multiple drives in parallel and wait for all jobs to finish:

    C:\Windows\System32\schtasks.exe /RUN /TN "chkdsk_G"
    C:\Windows\System32\schtasks.exe /RUN /TN "chkdsk_H"
    
    :loop
    
    for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn chkdsk_G /fo list ^| find "Status:"' ) do (
        if "%%f"=="Running" (
            ping -n 60 localhost >nul 2>nul
            goto loop
        )
    )
    
    for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn chkdsk_H /fo list ^| find "Status:"' ) do (
        if "%%f"=="Running" (
            ping -n 60 localhost >nul 2>nul
            goto loop
        )
    )
    
    :both jobs are now complete
    
    
    0 讨论(0)
  • 2021-01-04 15:05

    From batch file, query the task status, and if it is running, keep querying

    :loop
    for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
        if "%%f"=="Running" (
            ping -n 6 localhost >nul 2>nul
            goto loop
        )
    )
    
    0 讨论(0)
提交回复
热议问题