title: Run an async loop indefinitely from button click from Xamarin
body: <p>What is the best way to run a loop infinitely in parallel with the application?</p>
  
  <p>This is what I have tried so far:</p>
  
  <p>The button that actives the loop:</p>
  
  <pre><code>private void ActiveDeactiveTest(object sender, EventArgs e)
  {
      active = !active;            
  }
  </code></pre>
  
  <p>The loop:</p>
  
  <pre><code>bool runTest = false;
  bool active = false;
  public async void Test()
  {
      while (runTest)
      {
         if (active)
         {
             LblOutput.Text = "before";
             await Task.Delay(1000);
             LblOutput.Text = "after";
          }
          else
          {
             LblOutput.Text = "Idle";
          }
      }
  }
  </code></pre>
  
  <p>And the form instantiation:</p>
  
  <pre><code>public MainPage()
  {
      InitializeComponent();
      runTest = true;
      Test();
  }
  </code></pre>
  
  <p>Fairly new to this so any help would be appreciated
  Thanks in advance</p>