title: Windows Service running Async code not waiting on work to complete
body: <p><strong>In Brief</strong> </p>
  
  <p>I have a Windows Service that executes several jobs as async Tasks in parallel.  However, when the OnStop is called, it seems that these are all immediately terminated instead of being allowed to stop in a more gracious manner.</p>
  
  <p><strong>In more detail</strong></p>
  
  <p>Each job represents an iteration of work, so having completed its work the job then needs to run again.</p>
  
  <p>To accomplish this, I am writing a proof-of-concept Windows Service that:</p>
  
  <ul>
  <li>runs each job as an awaited async TPL Task (these are all I/O bound tasks)</li>
  <li>each job is run iteratively within a loop</li>
  <li>each job's loop is run in parallel</li>
  </ul>
  
  <p>When I run the Service, I see everything executing as I expect.  However, when I Stop the service, it seems that everything stops dead.</p>
  
  <p>Okay - so how is this working?</p>
  
  <p>In the Service I have a cancellation token, and a TaskCompletion Source:</p>
  
  <pre><code>private static CancellationTokenSource _cancelSource = new CancellationTokenSource();
  private TaskCompletionSource&lt;bool&gt; _jobCompletion = new TaskCompletionSource&lt;bool&gt;();
  private Task&lt;bool&gt; AllJobsCompleted { get { return _finalItems.Task; } }
  </code></pre>
  
  <p>The idea is that when every Job has gracefully stopped, then the Task AllJobsCompleted will be marked as completed.</p>
  
  <p>The OnStart simply starts running these jobs:</p>
  
  <pre><code>protected override async void OnStart(string[] args)
  {
      _cancelSource = new CancellationTokenSource();  
      var jobsToRun = GetJobsToRun(); // details of jobs not relevant 
      Task.Run(() =&gt; this.RunJobs(jobsToRun, _cancelSource.Token).ConfigureAwait(false), _cancelSource.Token);
  }
  </code></pre>
  
  <p>The Task RunJobs will run each job in a parallel loop:</p>
  
  <pre><code>private async Task RunModules(IEnumerable&lt;Jobs&gt; jobs, CancellationToken cancellationToken)
  {
      var parallelOptions = new ParallelOptions { CancellationToken = cancellationToken };    
      int jobsRunningCount = jobs.Count();
      object lockObject = new object();
  
      Parallel.ForEach(jobs, parallelOptions, async (job, loopState) =&gt;
      {
          try
          {
              do
              {
                  await job.DoWork().ConfigureAwait(false); // could take 5 seconds
                  parallelOptions.CancellationToken.ThrowIfCancellationRequested();
              }while(true);
          }
          catch(OperationCanceledException)
          {
              lock (lockObject) { jobsRunningCount --; }
          }
      }); 
  
      do
      {
          await Task.Delay(TimeSpan.FromSeconds(5));
      } while (modulesRunningCount &gt; 0);
  
      _jobCompletion.SetResult(true);
  }
  </code></pre>
  
  <p>So, what should be happening is that when each job finishes its current iteration, it should see that the cancellation has been signalled and it should then exit the loop and decrement the counter.</p>
  
  <p>Then, when jobsRunningCount reaches zero, then we update the TaskCompletionSource. (There may be a more elegant way of achieving this...)</p>
  
  <p>So, for the OnStop:</p>
  
  <pre><code>protected override async void OnStop()
  {
      this.RequestAdditionalTime(100000); // some large number        
      _cancelSource.Cancel();     
      TraceMessage("Task cancellation requested."); // Last thing traced
  
      try
      {
          bool allStopped = await this.AllJobsCompleted;          
          TraceMessage(string.Format("allStopped = '{0}'.", allStopped));
      }
      catch (Exception e)
      {
          TraceMessage(e.Message);
      }
  } 
  </code></pre>
  
  <p>What what I expect is this:</p>
  
  <ol>
  <li>Click [STOP] on the Service</li>
  <li>The Service should take sometime to stop</li>
  <li>I should see a trace statement "Task cancellation requested."</li>
  <li>I should see a trace statement saying either "allStopped = true", or the exception message</li>
  </ol>
  
  <p>And when I debug this using a WPF Form app, I get this.</p>
  
  <p>However, when I install it as a service:</p>
  
  <ol>
  <li>Click [STOP] on the Service</li>
  <li>The Service stops almost immediately</li>
  <li>I only see the trace statement "Task cancellation requested."</li>
  </ol>
  
  <p>What do I need to do to ensure the OnStop doesn't kill off my parallel async jobs and waits for the TaskCompletionSource?</p>