title: Create a Listener to Stop a Method Until Custom Callback Completes
body: <p>I would like to stop a method until a custom delegate provides a callback from one method to a listener in another in Xamarin.Android, but I am having a lot of trouble understanding the implementation of such a listener.</p>
  
  <p>I have only tried to understand how this could be done, but I'm stuck on how the delegate would be constructed and called.</p>
  
  <p>Let's say that I want this MainActivity method to stop </p>
  
  <pre><code>    public async override void OnWindowFocusChanged(bool hasFocus)
      {
             base.OnWindowFocusChanged(hasFocus);
  
             //how can I create a listener to stop this method until callback from OnPause?
             //await a delegate task?
  
      }
  </code></pre>
  
  <p>and await another method inside a Fragment:</p>
  
  <pre><code>    public override void OnResume()
      {
           base.OnResume();
  
           //I'd like to implement a delegate here to notify the OnWindowFocusChanged that OnResume method has been invoked
           //what would be the construction of such?
      }
  </code></pre>
  
  <p>How could I do that?</p>
  
  <p>From my research, I believe I would need to create a delegate task, and then await it's return in OnWindowFocusChanged.  However, I am confused about the implementation of such a solution.  If anyone could provide even a very basic pseudo-code example, it would help me out a lot.</p>
  
  <p>EDIT: I changed OnPause to OnResume because that's more useful for understanding what I'm trying to do.  The below answer references OnPause, but those two are extremely similar methods anyway.  </p>
  
  <p>Here is an example that <em>almost</em> does what I'm trying to do; except instead of starting my method when OnResume is invoked, OnWindowFocusChanged stops for 10000ms.  So basically, I'd like to replace the timer with a custom delegate (similar to an event) task that returns when Fragment.OnResume is invoked.</p>
  
  <pre><code>    public async override void OnWindowFocusChanged(bool hasFocus)
      {
           base.OnWindowFocusChanged(hasFocus);
  
           //we won't pass this line for 10000ms; how to replace with Task return?
           await WaitHere();
      }
  
  
      async Task WaitHere()
      {
          System.Threading.Thread.Sleep(10000);
      }
  </code></pre>