title: Running a long running function in a MVC Controller Asynchronously
body: <p>I'm working with Asp.net MVC 4, and I'm trying to have a function run in the background when the user clicks a link on a page.</p>
  
  <p>I have searched and searched, and I cannot figure out what to do.</p>
  
  <p>The function I am trying to run is a function inside the <code>PhoneNumbersController</code>, and it doesn't return anything, it just does a lot of work. </p>
  
  <p>Basically the user inputs a list of phone numbers (a very LARGE amount of numbers) and then clicks go. </p>
  
  <p>The server will then save all of those phone numbers to a DB. Then I want the server to run a function similar to this:</p>
  
  <pre><code>    Foo(int accountId)
      {
          var account = GetAccountById(accountId);
  
          while (account.IsRunning)
          {
              var Number = (from n in account.PhoneNumbers
                            where n.Status = "new"
                            select n).FirstOrDefault();
              // Do some work on Number.
  
              Thread.Sleep(1000); // I'm working with an API that limits you to 1 per second.
  
              account = GetAccountById(accountId);
          }
      }
  </code></pre>
  
  <p>I know, that code is terrible, but it's just to show an example. </p>
  
  <p>I'm going to be running through possibly at least 100,000 numbers, so we have to be able to pause and resume this (hence the while(IsRunning) variable. I have a pause function that will set that value to false.</p>
  
  <p>As someone who's never worked with asynchronous requests or multithreading before, what would be the best option here, and how would I go about it?</p>
  
  <p>Thanks in advance!</p>