title: Cancel parallel process
body: <p>I have a MainProcess.cs and ChildProcess.cs. Mainprocess is used to gather all required info and submit all data to parallel foreach loop. Child process will call to seperate API.</p>
  <p>MainProcess.cs</p>
  <pre><code>//MainProcess.cs
  
  string resourceId = GenerateUID();
  Parallel.ForEach(Accounts, parallelOptions, async (account) =&gt;
  {                
      await childProcess.ProcessMethod(resourceId, account);
  
  });
  </code></pre>
  <p>ChildProcess.cs</p>
  <pre><code>//ChildProcess.cs
  
  public async Task ProcessMethod(string resourceId, string account){
  
      var Req = {}; //Required info set in here.
  
      using (HttpClient client = new HttpClient())
      {
  
          var result = await client.PostAsJsonAsync($&quot;{baseUrl}api/services/app/Process/Run&quot;, Req);
          if (result.IsSuccessStatusCode)
          {
              //Save returned data to DB against the resourceId. (Figure 1)
          }
      }
  }
  </code></pre>
  <p>Data save a table like below.</p>
  <p><strong>Figure 1</strong></p>
  <p><a href="https://i.stack.imgur.com/ic2Z9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ic2Z9.jpg" alt="enter image description here" /></a></p>
  <p>There is MainProcess class and it has a method to run tasks parallelly.
  There is a ChildProcess class and it has &quot;ProcessMethod&quot;. Inside &quot;ProcessMethod&quot; I call to separate API to do some work. Above mention code and procedure is working 100% correctly.</p>
  <p>But my issue is, at a point I need to cancel processing. Lets use below &quot;figure 2&quot; to make this easier to understand.</p>
  <p><strong>Figure 2</strong></p>
  <p><a href="https://i.stack.imgur.com/I3GqF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I3GqF.jpg" alt="enter image description here" /></a></p>
  <p>According to above figure, total process will finish approximate in 10 seconds.
  At the 4th second, I need to cancel processing. (Call 1 and Call 5 already returned the response)
  Call 2, Call 3, Call 4 are sent the request, but still not received the response.</p>
  <p>What I need to do is, cancel all the processes at a given time.
  Simply It's like, I typed a Url in the browser and hit enter. before appearing the web page I close the browser. So I don't care about the responce.</p>
  <p>I try to do this with cancellation token. But where I stuck is, I couldn't cancel api calls for specific resourceId.</p>
  <p>Then I modified the code of ChildProcess.cs.</p>
  <p>added instance varibale</p>
  <pre><code>CancellationTokenSource tokenSource = new CancellationTokenSource();
  </code></pre>
  <p>then change</p>
  <pre><code>var result = await client.PostAsJsonAsync($&quot;{baseUrl}api/services/app/Process/Run&quot;, Req, tokenSource.Token);
  </code></pre>
  <p>I know, it should have to use below code for cancel process.</p>
  <pre><code>tokenSource.Cancel();
  </code></pre>
  <p>But the problem is how to cancel specific resourceId's process from a different function? (Cancel specific resourceId's process, because ProcessMethod can be called concurrently. If it is processing 2nd resourceId's process, It should not effect to 2nd process. 1st resourceId batch should have to close/cancel)</p>
  <p>Any expert can help me on this?</p>