title: How to use WebBrowser control DocumentCompleted event in C#?
body: <p>Before starting writing this question, i was trying to solve following</p>
  
  <pre><code>// 1. navigate to page
  // 2. wait until page is downloaded
  // 3. read and write some data from/to iframe 
  // 4. submit (post) form
  </code></pre>
  
  <p>The problem was, that if a iframe exists on a web page, DocumentCompleted event would get fired more then once (after each document has been completed). It was highly likely that program would have tried to read data from DOM that was not completed and naturally - fail.</p>
  
  <p>But suddenly while writing this question <strong><em>'What if' monster</em></strong> inspired me, and i fix'ed the problem, that i was trying to solve. As i failed Google'ing this, i thought it would be nice to post it here.</p>
  
  <pre><code>    private int iframe_counter = 1; // needs to be 1, to pass DCF test
      public bool isLazyMan = default(bool);
  
      /// &lt;summary&gt;
      /// LOCK to stop inspecting DOM before DCF
      /// &lt;/summary&gt;
      public void waitPolice() {
          while (isLazyMan) Application.DoEvents();
      }
  
      private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
          if(!e.TargetFrameName.Equals(""))
              iframe_counter --;
          isLazyMan = true;
      }
  
      private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
          if (!((WebBrowser)sender).Document.Url.Equals(e.Url))
              iframe_counter++;
          if (((WebBrowser)sender).Document.Window.Frames.Count &lt;= iframe_counter) {//DCF test
              DocumentCompletedFully((WebBrowser)sender,e);
              isLazyMan = false; 
          }
      }
  
      private void DocumentCompletedFully(WebBrowser sender, WebBrowserDocumentCompletedEventArgs e){
          //code here
      }
  </code></pre>
  
  <p>For now at least, my 5m hack seems to be working fine. </p>
  
  <p>Maybe i am really failing at querying google or MSDN, but i can not find:
  "How to use webbrowser control DocumentCompleted event in C# ?"</p>
  
  <p><strong>Remark:</strong> After learning a lot about webcontrol, I found that it does FuNKY stuff. </p>
  
  <p>Even if you detect that the document has completed, in most cases it wont stay like that forever. Page update can be done in several ways - frame refresh, ajax like request or server side push (you need to have some control that supports asynchronous communication and has html or JavaScript interop). Also some iframes will never load, so it's not best idea to wait for them forever. </p>
  
  <p>I ended up using:</p>
  
  <pre><code>if (e.Url != wb.Url)
  </code></pre>