为什么InternetExplorerClass ReadyState = tagREADYSTATE_COMPLETE不起作用?任何替代品?

时间:2014-01-10 13:58:16

标签: c# multithreading internet-explorer

当我调用_InternetExplorer.Navigate(....)并且只调用Thread.Sleep()10秒钟时,程序运行正常。 IE对象正确导航,我可以将InternetExlorerClass.Document保存为HTMLDocument。我试图尽可能快地完成过程。我正在尝试使用事件,例如DocumentComplete,这对我有利。如果我尝试使用DocumentComplete和ReadyState属性来调用保存HTMLDocument的函数,这将无法工作。 InternetExplorer.Document和保存的HTMLDocument始终不完整。这是为什么?任何帮助都会很好!

public void ThreadPoolCallback(Object threadContext)
    {
        Console.WriteLine("launching {0} Scraping...", threadContext.ToString());

        _InternetExplorerObject.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(BeforeNavigate2);
        _InternetExplorerObject.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(DocumentComplete);
        _InternetExplorerObject.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandler(NavigateError);

        _InternetExplorerObject.Navigate(_URL, ref missing, ref missing, ref missing, ref missing);

    }
    public void IE_SaveObjectAsHTML()
    {
        MyHTMLdoc = _InternetExplorerObject.Document as mshtml.HTMLDocumentClass;
        Console.WriteLine("");
        Console.WriteLine("The HTML Doc has saved");
        _DoneEvent.Set();
        _InternetExplorerObject.Quit();
    }


    private void BeforeNavigate2(object sender, ref object URL, ref object Flags, ref object Target, ref object PostData, ref object Headers, ref bool Cancel)
    {
        Console.WriteLine("Navigation Initiated");
    }
    private void DocumentComplete(object pDisp, ref object URL)
    {
        Console.WriteLine("Website Loaded");
        while (_InternetExplorerObject.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE)
        {
            Thread.Sleep(1000);
        }
        Console.WriteLine("Ready State Reached");
        IE_SaveObjectAsHTML();
    }
    private void NavigateError(object pDisp, ref object URL, ref object Target, ref object StatusCode, ref bool Cancel)
    {
        Console.WriteLine("ERROR : Navigation Failed. Refreshing");
        _InternetExplorerObject.Refresh();
        return;
    }




    public HTMLDocumentClass get_IE()
    {
        return MyHTMLdoc;
    }




    private System.Threading.EventWaitHandle myWaitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset);
    private string _URL;
    private InternetExplorer _InternetExplorerObject = new InternetExplorerClass();
    private object missing = System.Reflection.Missing.Value;
    private Type IEType = Type.GetTypeFromProgID("InternetExplorer.Application");
    private ManualResetEvent _DoneEvent;
    private HTMLDocumentClass MyHTMLdoc;

0 个答案:

没有答案