title: Wait for URL in WebBrowser to load C#
body: <p>I'm working on a program that automatically re-posts Craigslist posts in a batch form.</p>
  
  <p>I'm executing a method called <code>arrayTest()</code> that contains a line <code>webBrowser1.Navigate(urls[i]);</code>. This will get a URL from an Array and have the <code>webBrowser1</code> open it. After that is executed, the <code>clickRepost();</code> method is called. This method simulates a button click. But the method executes before the <code>webBrowser1</code> gets to finish opening the URL.</p>
  
  <p>What i need help on is telling the program to wait for the <code>webBrowser1</code> to finish opening the URL before moving on to the next method.</p>
  
  <p>What i have tried so far:</p>
  
  <pre><code>Task t = Task.Run(() =&gt; {
      webBrowser1.Navigate(urls[i]);
  })
  t.wait();
  clickRepost();
  </code></pre>
  
  <p>This still causes for <code>clickRepost()</code> to not let <code>webBrowser1</code> finish loading the URL. I also tried to add <code>System.Threading.Thread.Sleep(5000)</code> in between calling methods but as it does so in Java, it only shuts the program for x-Seconds.</p>
  
  <p>This is a segment of my program, really the only ones I'm having trouble with.</p>
  
  <pre><code>private void arrayTest()
      {
          String[] urls = CLLinks.Text.Split('\n');
          for (int i = 0; i &lt; urls.Length; i++)
          {
              log.AppendText("Opening Url...\n");
              webBrowser1.Navigate(urls[i]);
              log.AppendText("Reposting...\n");
              clickRepost();
              log.AppendText("Checking Completion...");
              webBrowser1.Navigate(urls[i]);
              completionCheck();
          }
      }
  
      private void CLRenBtn_Click(object sender, EventArgs e)
      {
          arrayTest();
      }
  
      private void completionCheck()
      {
          HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");
  
          foreach (HtmlElement item in col)
          {
              if (!(item.GetAttribute("value") == "Renew this Posting"))
              {
                  log.AppendText("Renew Posting does not exist");
              }
              else
              { }
          }
      }
  
      private void clickRepost()
      {
          HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");
  
          foreach (HtmlElement item in col)
          {
              if (item.GetAttribute("value") == "Renew this Posting")
              {
                  item.InvokeMember("click");
              }
              else
              {}
          }
      }
  </code></pre>
  
  <p><code>CLRenBtn_Click</code> Is the button i press on the form to start the Re-posting. <code>arrayTest()</code> will be changed to a more relevant name. At the time i was just testing whether this would work. And im planning on merging <code>clickRepost()</code> and <code>completionCheck()</code> since they do the same thing.</p>
  
  <p>Many thanks in advance!</p>