title: How to determine whether Task.Run is completed within a loop
body: <p>This may be an odd question and it is really for my educational purpose so I can apply it in future scenarios that may come up.</p>
  
  <p>I am using C#.</p>
  
  <p>I am stress testing so this is not quite production code.</p>
  
  <p>I upload data to my server via a web service.</p>
  
  <p>I start the service off using a Run.Task.</p>
  
  <p>I check to see if the Task is completed before allowing the next Run.Task to begin.</p>
  
  <p>This is done within a loop.</p>
  
  <p>However, because I am using a modular declared Task will not the result be affected?
  I could declare a local Task.Run variable but I want to see how far I can get with this question 1st.</p>
  
  <p>If the Task.Run can raise an event to say it is completed then this may not be an issue?</p>
  
  <p>This is my code:</p>
  
  <p>//module declaration:</p>
  
  <pre><code>private static Task webTask = Task.Run(() =&gt; { System.Windows.Forms.Application.DoEvents(); });
  </code></pre>
  
  <p>//in a function called via a timer</p>
  
  <pre><code>if (webTask.IsCompleted)
  {
     //keep count of competed tasks
  }
  
  
  webTask = Task.Run(() =&gt;
  {
      try 
      { 
           wcf.UploadMotionDynamicRaw(bytes);  //my web service
      }
      catch (Exception ex)
      {
          //deal with error
      }
  );
  </code></pre>