title: How to load a new page while asynchronously executing a thread in background
body: <p>In my C# controller, I kicked off an async task (using Task.Run), then redirect to a different page. My goal was to have the page fully loaded while the async task was running in the background. There will be a javascript code keep pinging web server to see if the job is done. However, what I noticed was that the code executes the redirect line before executing the loading in the async thread which is good. However, my page didn't start to load until the async thread is finished (even after redirect line finishes executing).</p>
  
  <p>I tried</p>
  
  <pre><code>Task.Run(() =&gt; someLoadingCode());
  
  HostingEnvironment.QueueBackgroundWorkItem(a =&gt; someLoadingCode());
  
  new Thread(() =&gt; someLoadingCode()) { IsBackground = true }.Start();
  </code></pre>
  
  <p>but none of them works.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public void Controller()
  {
      Task.Run(() =&gt; someLoadingCode());
      Response.Redirect(url);
  }
  </code></pre>
  
  <p>Since someLoadingCode can take 10 minutes, I wanted it to redirect to my new page while it's loading in background. User may even be able to click away and go to another page after redirecting. But now it won't load my new page before someLoadingCode is finished.</p>