title: C# Threading/Async: Running a task in the background while UI is interactable
body: <p>After looking around on both Async/Await and Threading, I'm still unsure of the right way to apply it to my situation. No matter the variation that I try my UI still hangs because I don't seem to be calling my desired function asynchronously, additionally, I may in fact need threading for my solution. </p>
  
  <p><strong>What I'm trying to do:</strong> I have a WPF application on which there is a button that I would like to start an operation that still allows interaction with the program, through UI or otherwise. Once a condition is met that is determined outside of this function, the function should end. To me this sounds fairly standard but I have a feeling I'm misunderstanding something and I've implemented it incorrectly.</p>
  
  <p><strong>What I have right now:</strong> </p>
  
  <pre><code>private async void start_button_Click(object sender, RoutedEventArgs e)
  {
      await StaticClass.MyFunction();
  }
  
  private void stop_button_Click(object sender, RoutedEventArgs e)
  {
      StaticClass.stopFlag = true;
  }
  
  public static Task&lt;int&gt; myFunction()
  {
      //Stuff Happens
  
      while(StaticClass.stopFlag == false)
          //Do Stuff
  
      //Stuff Happens
  
      return Task.FromResult(1) //I know this is bad, part of the reason I'm asking
  }
  </code></pre>
  
  <p>I was hoping for some guidance on if I'm approaching this the right way and any insight on what I'm doing wrong.</p>