Click to See Complete Forum and Search --> : Using AxWebBrowser From a Thread


hujanais
January 1st, 2005, 05:55 PM
Hi there,

I have an application written in C#.NET that uses the AxWebBrowser component. The program basically can navigate to some webpage and automatically parse the HTML tags and fill up some forms and send the form. This program has a worker thread that continously polls the webpages for data changes and when certain conditions are met, the thread "presses" a button on the webpage to get more data. At this point, everything still works very well.

The problem occurs when after the new data has been updated on the screen, no documentcomplete event is fired so my thread function essentially wants to do a
IHTMLDocument2 htmlDoc = (IHTMLDocument2)axWebBrowser1.Document; and reparse the data. However, to my surprise, the axWebBrowser1 object is undefined when it is trying to be used from the thread.

I tried using delegates but the problem is still not solved. The axWebBrowser1 control is always undefined.

delegate bool MyFunctionDelegate();

public bool MyFunction()
{
if( axWebBrowser1.InvokeRequired == false )
{
IHTMLDocument2 htmlDoc = (IHTMLDocument2)axWebBrowser1.Document;
// Do the parsing of the new data.

else
{
MyFunctionDelegate myDelegate = new MyFunctionDelegate (EnumerateBettingInputElements);
BeginInvoke(enumDelegate, new object[] {} );
}
return true;
}


Please help. I have tried many things and couldn't figure it out. Thanks in advance.

Sincerely,
Teik Lee

Sahir
January 1st, 2005, 10:28 PM
the axWebBrowser1 object is undefined when it is trying to be used from the thread.


Are you sure? It is more likely that axWebBrowser1.Document is undefined.

hujanais
January 1st, 2005, 11:19 PM
Hi Sahir,

How are you? Glad you replied. No, I am pretty sure the document exists.

When I call the function locally from the same thread that I created the WebBrowser object, everything works great. But once I call it from a thread. AxWebBrowser is undefined.

Very strange and I must be missing something obvious.

Hujan

hujanais
January 1st, 2005, 11:50 PM
Ok. Very strange.

When I use the debugger axWebBrowser1 always reads undefined but it works. I do not understand. Earlier it was failing, not it it fine but I do not understand the watch in the debugger saying that it is undefined.

Anyhow, all this rambling probably doesn't make any sense, so I have attached a C# project for those interested to take a look.

Hujan

Sahir
January 2nd, 2005, 02:40 AM
Hello Hujan,
Re : "Earlier it was failing, now it is fine".

If you try to use the Document object before the page loads fully you may get an error. My guess is it works because the page is loading much faster now.

Rgds
S

hujanais
January 2nd, 2005, 10:23 AM
Hi Sahir,

No, the page does load up in time. It seem to fail when I use the debugger but when I let it to free run, it works everytime. This is the first time I have this situation in reverse.

Anyhow, this problem is resolved for now. Thanks your help.

Hujan

jit
April 6th, 2005, 09:08 AM
Hi.
I had a similar problem as yours.
I discovered that on some machine this didn't happen.
This occured because I tried to read a document before completing load a multiframe page: setting properly the target parameter in Navigate method, everything goes well.
Another solution is to wait until the navigation is completed, paying attention to a frameset scenario (multiple navigation complete).
JIT