title: Is there any way to know if a method is running being awaited from within the method?
body: <p>I'm a university student but, since I like programming, I try to create a library of code that's been useful to me (something like a code base).</p>
  <p>In the process of doing this, I started designing/writing an asynchronous method that's about to be used for interlocking a variable. My goal is to produce different result when this method is being awaited (runs synchronously) and when it isn't.</p>
  <p>An example could be the following:</p>
  <pre class="lang-cs prettyprint-override"><code>
  private int _lock;
  
  public async Task&lt;bool&gt; Lock()
  {
      if (method_is_not_being_awaited)
          return Interlocked.Exchange(ref _lock, 1) == 0;
  
      while (0 != Interlocked.Exchange(ref _lock, 1)) {}
      return true;
  
  }
  </code></pre>
  <p>Is there any way to achieve such result? If yes, how?</p>
  <p>ps: I know that I could make 2 different methods <code>bool lock()</code> and <code>async Task&lt;bool&gt; lockAsync()</code> but, that's not what I ask for</p>