Webbrowser, htmldocument and documentComplete event

K

Kiks

Hello, I've a question about a System.Windows.Forms.WebBrowser class that
I've instancied into my DLL.

I would like to navigate to a page and catch the
System.Windows.Forms.HtmlDocument that my WebBrowser Navigated to.



I have tried different solutions, but I found problems in them all:



In this first solution, always my function ApriIE return NULL, and the
WebBrowser seems to open the URL only after my application finishes.



private System.Windows.Forms.HtmlDocument ApriIE()

{

String URL = https://www.cartasi.it;

System.Windows.Forms.WebBrowser browser = new
System.Windows.Forms.WebBrowser();

Exception e = new Exception();

DateTime start = DateTime.Now;

TimeSpan timeout = new TimeSpan(0, 0, 10);

browser.Navigate(URL, true);


try

{

browser.Navigate(vPostehRef, true);

while (browser.IsBusy)

{

System.Windows.Forms.Application.DoEvents();

if (DateTime.Now - start > timeout) throw e;

}

return browser.Document;

}

catch

{

return null;

}



In this second solution, the browser_DocumentCompleted Event never fires:



private System.Windows.Forms.HtmlDocument ApriIE()

{

System.Windows.Forms.WebBrowser browser = new
System.Windows.Forms.WebBrowser();

browser.DocumentCompleted+=new
System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

Exception e = new Exception();

DateTime start = DateTime.Now;

TimeSpan timeout = new TimeSpan(0, 0, 10);

browser.Navigate(vPostehRef, true);


try

{

browser.Navigate(vPostehRef, true);

while (browser.IsBusy)

{

System.Windows.Forms.Application.DoEvents();

if (DateTime.Now - start > timeout) throw e;

}

return browser.Document;

}

catch

{

return null;

}

}

private void browser_DocumentCompleted( Object sender,
System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)

{

doc = (System.Windows.Forms.HtmlDocument)sender;

}



Any ideas?

Thanks to all,

Kiks
 
M

Marc Gravell

WebBrowser is intended to be used as a winform control, and will delay
loading until it is displayed. Since you aren't hosting it anywhere, I
wouldn't expect it to do anything.

If your dll is used from a winform app, then it could perhaps create
an invisible (offscreen? transparent?) form [I suspect Visible=false
will not work] for hosting the WebBrowser; however, this would be an
inappropriate solution if the dll is used from a service app
(including ASP.NET).

If you are using a service app, perhaps use WebClient to do the HTTP
request, and then parse the response text.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top