title: async/await statement doesn't work correctly in AspNet-MVC, but works in console app
body: <p><em>Background: I have a long linq query to database in my web-app. So I want to show a user a query-timer. I tried to do this through async/await statements, but I stuck. (I use signalR for real-time mode)</em></p>
  
  <p>A peace of my test-code:</p>
  
  <p><strong>MyHub.cs</strong>:</p>
  
  <pre><code>public class MyHub : Hub
      {
  
          public void Message(string message)
          {
              Clients.All.message(message);
          }
  
          public void Tick(int value)
          {
              Thread.Sleep(1000);
              Clients.Caller.tick(value);
          }
  
          public void StartAsyncMethod(int value)
          {
              Clients.All.message("&lt;br&gt; M1: StartAsyncMethod started!");
              int t = 0;
              MyAsyncClass myAsyncClass = new MyAsyncClass();
              Task task = myAsyncClass.AsyncMethod(value*1000);
              Clients.All.message("&lt;br&gt; M1: Start While...");
  
              while (!task.IsCompleted)
              {
                  t++;
                  Tick(t);
              }
  
              Clients.All.message("&lt;br&gt; M1: StartAsyncMethod Finished!");
  
          }
      }
  
  </code></pre>
  
  <p><strong>MyAsyncClass.cs</strong> </p>
  
  <pre><code>    public class MyAsyncClass
      {
          public async Task AsyncMethod(int _i)
          {
              var hub = GlobalHost.ConnectionManager.GetHubContext&lt;MyHub&gt;();
              hub.Clients.All.message($"&lt;br&gt;==M2== Wait for {_i} milliseconds");
              await Task.Run(() =&gt; Sleep(_i));
              hub.Clients.All.message($"&lt;br&gt;==M2== Finish inner method...");
          }
  
          private void Sleep(int value)
          {
              Thread.Sleep(value);
          }
      }
  
  </code></pre>
  
  <p>And the point is in <code>while (!task.IsCompleted)</code> - the app stacks on that state:</p>
  
  <p><a href="https://i.stack.imgur.com/MLRe3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MLRe3.jpg" alt="app state whith while-statement"></a></p>
  
  <p>If <code>while</code> is commented, it works fine (but without timer of course):</p>
  
  <p><a href="https://i.stack.imgur.com/Yev7J.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yev7J.jpg" alt="app state without while-statement"></a></p>
  
  <p><strong>More than that</strong> - in simple console application this code works in both variants!</p>
  
  <p>Code:</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              int i = 5;
              Console.WriteLine($"Main : Start StartAsyncMethod({i})");
              StartAsyncMethod(i);
              Console.WriteLine($"Main : FINISH StartAsyncMethod({i})");
              Console.WriteLine("\n\nPress any key to exit....");
              Console.ReadKey();
          }
  
          public static void StartAsyncMethod(int value)
          {
              Console.WriteLine("  M1 : StartAsyncMethod started!");
              int timer = 0;
              MyAsyncClass myAsyncClass = new MyAsyncClass();
              Task task = myAsyncClass.AsyncMethod(value * 1000);
              //while (!task.IsCompleted)
              //{
              //    timer++;
              //    Thread.Sleep(1000);
              //    Console.WriteLine("  M1 : \"hello\" from While cicle ... ");
              //}
              Console.WriteLine("  M1 : StartAsyncMethod FINISHED!");
          }
      }
  
      public class MyAsyncClass
      {
          public async Task AsyncMethod(int _i)
          {
              Console.WriteLine($"    M2 : Wait for {_i} milliseconds");
              await Task.Run(() =&gt; Sleep(_i));
              Console.WriteLine($"    M2 : Finish inner method...");
          }
  
          private void Sleep(int v)
          {
              Console.WriteLine($"    M2 : Sleep for {v} mls");
              Thread.Sleep(v);
          }
      }
  
  </code></pre>
  
  <p>Outputs:</p>
  
  <p>With <code>while</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/7ucpQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7ucpQ.jpg" alt="enter image description here"></a></p>
  
  <p>Without <code>while</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/vGoIz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vGoIz.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Question:</strong> does this code really works in different ways in ASPnet and ConsoleApp, or I have missed something? </p>
  
  <p>Thanks.</p>