none
Exception occurs when IHTMLWindow2 window = theDoc.parentWindow; RRS feed

  • Question

  • As title,

    I try to execute a javascript with HDocVw.InternetExplorerClass so after the documentComplete event, I first get the reference to IHTMLWindow2 window = theDoc.parentWindow; and then window.execScript("goSearch()", "JavaScript");.

     

    However, IHTMLWindow2 window = theDoc.parentWindow throws a exception:

    System.InvalidCastException: Specified cast is not valid.
       at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
       at mshtml.HTMLDocumentClass.get_parentWindow()

     

    I have no idea about the reason of this excpetion. Could any experienced one provide me some guidelines to fix this error?

     

    Thanks a million.

    Ricky.

    Thursday, October 25, 2007 12:47 AM

Answers

  • The problem is you can't make calls from outside the thread that created the Document object. In other words, you must make Document calls from within the same thread.

    So instead of threadpool, gonna have to fake it with some kind of array of STA threads. Ugh...!





    Thursday, October 25, 2007 6:10 PM
  • this is what worked for me:

    ManualResetEvent[] aDoneEvents = new ManualResetEvent[_MaxThreads];

             for (int i = 0; i < aDoneEvents.Length; i++)
                aDoneEventsIdea = new ManualResetEvent(false);
            
    //init aDataForThread

             for (int i = 0; i < _MaxThreads; i++)
             {
                Thread aThread = new Thread(new ParameterizedThreadStart(ThreadWorker));
                aThread.SetApartmentState(ApartmentState.STA);
                aThread.IsBackground = true;
                aThread.Start(new ThreadData(aDataForThread, aDoneEventsIdea));
             } // for

             WaitAll(aDoneEvents);


    Then inside the ThreadWorker method you initialize your Browser object, make your Document calls, etc...

    Bottom line is you can't use ThreadPool and IE because of COMpartmentalization.
    Friday, October 26, 2007 1:17 PM

All replies

  • I get the same error doing the same thing you're trying to do.

    Funny thing is, I have a non-threaded version of the app where my code does not fail, but the threaded version does fail on that code.

    Looks like theDoc doen't get fully intialized. If you'll notice, Parent Window and Frames, and some other properties have exceptions.

    If anyone has any hints to help out, that would be great!

    Thanks!
    Thursday, October 25, 2007 4:10 PM
  • The problem is you can't make calls from outside the thread that created the Document object. In other words, you must make Document calls from within the same thread.

    So instead of threadpool, gonna have to fake it with some kind of array of STA threads. Ugh...!





    Thursday, October 25, 2007 6:10 PM
  • Thanks for the help, Phrank.

     

    Could you give a concrete code sample to demonstrate the solution you described?

     

    Regards,

    Ricky.

     

    Friday, October 26, 2007 12:30 AM
  • this is what worked for me:

    ManualResetEvent[] aDoneEvents = new ManualResetEvent[_MaxThreads];

             for (int i = 0; i < aDoneEvents.Length; i++)
                aDoneEventsIdea = new ManualResetEvent(false);
            
    //init aDataForThread

             for (int i = 0; i < _MaxThreads; i++)
             {
                Thread aThread = new Thread(new ParameterizedThreadStart(ThreadWorker));
                aThread.SetApartmentState(ApartmentState.STA);
                aThread.IsBackground = true;
                aThread.Start(new ThreadData(aDataForThread, aDoneEventsIdea));
             } // for

             WaitAll(aDoneEvents);


    Then inside the ThreadWorker method you initialize your Browser object, make your Document calls, etc...

    Bottom line is you can't use ThreadPool and IE because of COMpartmentalization.
    Friday, October 26, 2007 1:17 PM
  • Thanks again,

    Ricky.

    Friday, October 26, 2007 2:31 PM
  • Hi ,

    I have a lot a problems b/c of the Multi-Threading anyone have a full solution that works ?
    I try :  PhrankBooth soloutions, but its doesn't seems to work well for me,
    maybe i didn't implment it well .


    Thanks
    Naty
    Monday, October 26, 2009 9:01 AM
  •    private WebBrowser _webBrowser; //initialize this somewhere
    
       private void ExecuteJavaScript()
       {
         Thread aThread = new Thread(ExecuteJavaScriptWorker);
         aThread.SetApartmentState(ApartmentState.STA);
         aThread.Start(); 
       }
    
       private void ExecuteJavaScriptWorker()
       {
         HTMLDocument _document = _webBrowser.Document;
         _document.parentWindow.execScript("alert('Arbitrary javascript code')", "javascript");
       }
    Thursday, May 19, 2011 5:14 PM
  • Worked For Me
    Monday, July 16, 2018 6:29 AM