title: C# async web browser runs very slow
body: <p>this program  reads a list of web site then saves them.
  i found it runs good for the first 2 url requests. then goes very slow (about 5 min per request)</p>
  
  <p>the time spend on row 1 and row 2 are only 2 second.
  Then all other will be about 5 min each.</p>
  
  <p>When i debug , i see it actually tooks  long in              <code>wb.Navigate(url.ToString());</code></p>
  
  <pre><code>    public static async Task&lt;bool&gt; test()
      {
  
          long totalCnt = rows.Count();
          long procCnt = 0;
          foreach (string url in rows)
          {
              procCnt++;
  
              string webStr = load_WebStr(url).Result;
              Console.WriteLine(DateTime.Now+ "["+procCnt + "/" + totalCnt+"]  "+url);
          }
  
  
  
          return true;
      }
  
  
  public static async Task&lt;string&gt; load_WebStr(string url)
  {
      var tcs = new TaskCompletionSource&lt;string&gt;();
  
      var thread = new Thread(() =&gt;
      {
          EventHandler idleHandler = null;
  
          idleHandler = async (s, e) =&gt;
          {
              // handle Application.Idle just once
              Application.Idle -= idleHandler;
  
              // return to the message loop
              await Task.Yield();
  
              // and continue asynchronously
              // propogate the result or exception
              try
              {
                  var result = await webBrowser_Async(url);
                  tcs.SetResult(result);
              }
              catch (Exception ex)
              {
                  tcs.SetException(ex);
              }
  
              // signal to exit the message loop
              // Application.Run will exit at this point
              Application.ExitThread();
          };
  
          // handle Application.Idle just once
          // to make sure we're inside the message loop
          // and SynchronizationContext has been correctly installed
          Application.Idle += idleHandler;
          Application.Run();
      });
  
      // set STA model for the new thread
      thread.SetApartmentState(ApartmentState.STA);
  
      // start the thread and await for the task
      thread.Start();
      try
      {
          return await tcs.Task;
      }
      finally
      {
          thread.Join();
      }
  
  }
  
  
  public static async Task&lt;string&gt; webBrowser_Async(string url)
  {
  
      string result = "";
      using (var wb = new WebBrowser())
      {
          wb.ScriptErrorsSuppressed = true;
  
          TaskCompletionSource&lt;bool&gt; tcs = null;
          WebBrowserDocumentCompletedEventHandler documentCompletedHandler = (s, e) =&gt;
          tcs.TrySetResult(true);
  
  
          tcs = new TaskCompletionSource&lt;bool&gt;();
          wb.DocumentCompleted += documentCompletedHandler;
          try
          {
              wb.Navigate(url.ToString());
              // await for DocumentCompleted
              await tcs.Task;
          }
          catch
          {
              Console.WriteLine("BUG!");
  
          }
          finally
          {
              wb.DocumentCompleted -= documentCompletedHandler;
          }
          // the DOM is ready
  
          result = wb.DocumentText;
  
      }
  
  
      return result;
  }
  </code></pre>