Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:48:03+00:00 2026-05-27T04:48:03+00:00

Why does DocumentComplete event of WebBrowser COM object fire before page is loaded? I

  • 0

Why does DocumentComplete event of WebBrowser COM object fire before page is loaded? I thought that this event is only fired when page is fully rendered in browser’s window.

this is my BHO implementation:

[ComVisible(true),
Guid("5a954357-44bd-4660-9570-17bb1b71eeaa"),
ClassInterface(ClassInterfaceType.None)]
public class BHO : IObjectWithSite
{
    private WebBrowser browser;
    private DateTime startTime;
    private DateTime endTime;
    private object _pUnkSite;

    public void OnDocumentComplete(object pDisp, ref object URL)
    {
        if (!ReferenceEquals(pDisp, _pUnkSite))
        {
            return;
        }

        using (StreamWriter sw = File.AppendText("log_path"))
        {
            endTime = DateTime.Now;
            TimeSpan ts = endTime.Subtract(startTime);
            sw.WriteLine("completed in {0}.{1}", ts.Seconds, ts.Milliseconds);
        }

    }

    public void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
    {
        if (!ReferenceEquals(pDisp, _pUnkSite))
        {
            return;
        }
        startTime = DateTime.Now;
    }

    public int SetSite(object site)
    {
        if (site != null)
        {
            _pUnkSite = site;
            browser = (WebBrowser)site;   
            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
            browser.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);
        }
        else
        {
            browser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
            browser.BeforeNavigate2 -= new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);
            browser = null;

        }
        return 0;
    }

    public int GetSite(ref Guid guid, out IntPtr ppvSite)
    {
        IntPtr punk = Marshal.GetIUnknownForObject(browser);
        int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
        Marshal.Release(punk);

        return hr;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:48:04+00:00Added an answer on May 27, 2026 at 4:48 am

    Because there are other documents on a page. An iframe, or an image, for example, will fire the DocumentComplete event. What you need to do is ensure that the object that raised DocumentComplete is the actual page. For example:

    private void _webBrowser2Events_DocumentComplete(object pdisp, ref object url)
    {
        if (!ReferenceEquals(pdisp, _pUnkSite))
        {
            //Exit, because the DocumentComplete is not the document complete for the page.
            return;
        }
        //Do your normal stuff here
    }
    

    Where _pUnkSite is the site that was passed in from SetSite.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does a firewall running on a machine only block stuff from outside that machine,
Does anyone know the function of the command vmware-vim-cmd vimsvc/connect (vimid). I thought that
It seems that my program does not handle the DownloadComplete event. Can someone guide
Does this line of Perl really do anything? $variable =~ s/^(\d+)\b/$1/sg; The only thing
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most
Does C# have built-in support for parsing strings of page numbers? By page numbers,
Before starting writing this question, i was trying to solve following // 1. navigate
I have a page that refreshes every 20 seconds and I need to know
Does anyone know how I can implement a single Touch Event. A simple, one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.