7
AutoResetEvent receiver = new AutoResetEvent(false);
Thread t = null;
            t = new Thread(new ThreadStart(() =>
            {

                while (Browser.ReadyState != WebBrowserReadyState.Complete) // error
                {
                    Thread.Sleep(10);
                }
                receiver.Set();
                t.Abort();
            }));
            t.Start();

            // Timeout
            success = receiver.WaitOne(10000);

Browser is

public WebBrowser Browser { get; set; }

Screenshot

I don't really understand why I get such error. The Browser.ReadyState is a enum type WebBrowserReadyState

So what do you think?

EDIT: First: SS1 Second: SS2

user1046403
  • 555
  • 1
  • 5
  • 11
  • 1
    Whats the full name? (i.e System.Windows.forms.WebBrowserReadyState) I suspect these are different – Sayse Aug 14 '13 at 13:03
  • 1
    I think you should get rid of `t.Abort();`. You don't need it. – H H Aug 14 '13 at 13:03
  • 3
    Probably the exception is *inside* `Browser.ReadyState`, not in your method. – xanatos Aug 14 '13 at 13:04
  • @Sayse check screenshots, CodeCaster I'm checking it right now – user1046403 Aug 14 '13 at 13:08
  • Definitely duplicate question then – Sayse Aug 14 '13 at 13:10
  • @CodeCaster I tried invoking it but when I invoke webbrowser things get worse I'm getting really horrible errors like HRESULT returned E_FAIL or COM component errors etc. etc. – user1046403 Aug 14 '13 at 13:11
  • I think I should stick with invoking thread and try to handle other errors. – user1046403 Aug 14 '13 at 13:13
  • 2
    I'm curious why you are doing this in a thread.. given that the browser will navigate asynchronously anyway.. – Simon Whitehead Aug 14 '13 at 13:14
  • Actually I'm trying to catch when this browser FULLY completes loading I mean sometimes it raises the DocumentCompleted event multiple times and I found after my researchs that the ReadyState set Completed after it FULLY loads. – user1046403 Aug 14 '13 at 13:17
  • Aaand my "possible duplicate"-comment was deleted again, even though I edited it. [FIX IT](http://meta.stackexchange.com/questions/141414/dont-delete-comments-with-link-to-duplicate-if-they-are-modified). – CodeCaster Aug 14 '13 at 13:32
  • 2
    The InvalidCastException is raised because the Document property cannot be accessed in a worker thread. Used by the ReadState property getter. The underlying call that fails is IUnknown::QueryInterface() for the IMarshal interface. The real solution you are looking for [is here](http://stackoverflow.com/a/3239313/17034) – Hans Passant Aug 14 '13 at 13:49

0 Answers0