title: How do you wait on a Task Scheduler task to finish in a batch file or C#?
body: <p>I am trying to write a batch file that does two things:</p>
  
  <ol>
  <li>First it launches an installer (install.exe) which installs a program (program.exe).</li>
  <li>Second it launches an instance of the installed program (program.exe). This must be executed after the installation completes.</li>
  </ol>
  
  <p>This would be relatively straightforward except that the installer needs administrator privileges and must run in a user context. Even with these restrictions this would still be relatively straightforward except that I am running this on an Azure Worker Role, which means two things:</p>
  
  <ol>
  <li><a href="http://social.msdn.microsoft.com/Forums/windowsazure/en-US/ec40165c-4f54-49ec-a3a2-5dea02b04231/launching-elevated-process-during-onstart" rel="noreferrer">Elevated batch files must be run from a startup task.</a></li>
  <li><a href="http://blog.smarx.com/posts/windows-azure-startup-tasks-tips-tricks-and-gotchas" rel="noreferrer">There is no user context for startup tasks in Azure worker roles.</a></li>
  </ol>
  
  <p>It therefore appears that the solution is to <a href="http://www.davidaiken.com/2011/01/19/running-azure-startup-tasks-as-a-real-user/" rel="noreferrer">run the installer as a Task Scheduler task in a real user context</a>. However, this comes with the caveat that I would then need to wait for this task to finish before I could launch program.exe.</p>
  
  <p>Thus, my question is: How do I wait on a Task Scheduler task to finish in a batch file?</p>
  
  <p>Alternatively, I could daisy chain the two calls in my batch file within a single Task Scheduler task (Task Scheduler supports up to 16 sequential events in a single task [Citation Needed]).</p>
  
  <p>However, if I take this approach my batch file will terminate as soon as the task is <em>scheduled</em>, not as soon as it <em>finishes</em>. Unfortunately, I must wait on it to finish before I can do any of the logic in my Worker Role. However, if I can check if a Task Scheduler task has finished from C# (WITHOUT admin privileges), then I can just wait on it there.</p>
  
  <p>Thus a second viable answer would be to the question: How do I check if a Task Scheduler task has completed from C#?</p>
  
  <p><strong>EDIT: I thought I would clarify the below answer by MC ND a bit:</strong></p>
  
  <p>From a high level, this command checks to see if the task is running, and then it sleeps and loops if it is.</p>
  
  <pre><code>:loop
  for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
      if "%%f"=="Running" (
          ping -n 6 localhost &gt;nul 2&gt;nul
          goto loop
      )
  )
  </code></pre>
  
  <p>The call <code>schtasks /query /tn yourTaskName /fo list</code> outputs something like the following:</p>
  
  <pre><code>Folder: \  
  HostName:      1234567890  
  TaskName:      \yourTaskName  
  Next Run Time: 11/27/2030 11:40:00 PM  
  Status:        Ready  
  Logon Mode:    Interactive/Background 
  </code></pre>
  
  <p>The for command loops over each line in the output. Each line is split into two tokens using the delimiter ":". If a line starting with "Status" is found, then the token after the delimiter in that line is stored in the variable f (which in the example output above would be "Ready").</p>
  
  <p>The variable f is then checked to see if it matches the status "Running". If the status is not "Running" it is assumed that the task has completed or failed, and the loop exits. If the status is "Running", then the task is still running and the batch script pauses for a short time before checking again. This is accomplished by making a ping call, since batch does not have a sleep command. Once the script is done sleeping it restarts this sequence.</p>