title: Is it possible to convert a `ref bool` to a CancellationToken?
body: <p>I have a legacy scenario where a <code>ref bool</code> was being used to send a cancellation signal to an implementation. Now, I want to call a <code>Task</code>-based library method that takes a <code>CancellationToken</code> instance, which I also want to be cancelled when the boolean changes value.</p>
  
  <p>This is what I have to work with:</p>
  
  <pre><code>void Method(ref bool isCancelled)
  {
      while (!isCancelled)
      {
          ...
          DoThis();
          DoThat();
          ...
      }
  }
  </code></pre>
  
  <p>And this is what I want to do:</p>
  
  <pre><code>Task MethodAsync(ref bool isCancelled)
  {
      while (!isCancelled)
      {
          ...
          DoThis();
          await DoTheNewThingAsync(isCancelled.ToCancellationToken());
          DoThat();
          ...
      }
  }
  </code></pre>
  
  <p><code>ToCancellationToken()</code> doesn't exist in this context of course, and is used just to show the intent.</p>
  
  <p>I tried to create a custom implementation of <code>CancellationTokenSource</code> but there is nothing virtual in the class that I could work with. It's also not possible to create a custom <code>CancellationToken</code> directly since it is a <code>struct</code> and cannot be inherited.</p>
  
  <p>I'm aware that using a <code>ref bool</code> is a poor practice but I can't currently change the underlying implementation that relies on it, so I need a way to use it's value as the cancellation mechanism for the task-based call.</p>