Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
4.29/5 (6 votes)
See more:
Hi guys,

I want to use in simple class ( not winform). I think that i must use thread to run WebBowser.
I have an exception between webbrowser and thread

Public Class CScenTest
Private Dim m_WebBrowser As WebBrowser
Private Dim m_test as Boolean

private sub init
 AddHandler m_WebBrowser.DocumentCompleted, AddressOf m_WebBrowser_DocumentCompleted
end sub


Private Sub Execute()        
    Dim l_Thread As Thread
    l_Thread = New Thread(AddressOf navigeUrl)
    l_Thread.Start()
End Sub

private Sub m_WebBrowser_DocumentCompleted(sender As object, e As WebBrowserDocumentCompletedEventArgs)
   'I want to go here
   m_test = true
End Sub


Private sub navigeUrl()
   m_WebBrowser.Navigate("www.yahoo.com")
End Sub

End Class
Posted
Comments
Manfred Rudolf Bihy 16-May-11 14:42pm    
Please have a look at my solution. It is not VB.net but in C#. It should give you the right idea on how to solve your problem though.

Cheers!

After scratching my head some over the various responses the application gave me during debugging and then googling some on the internets I finally came up with this solution that works without a form to host your webbrowser control. Since I'm not used to writing programs in VB.NET I'll give you my work in C#. If you have any difficulties translating it into VB.net maybe you'll find some assistance here on CP. Anyhow trying it out in C# should not pose a problem. So many words, here is the code:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WebBrowserWithoutAForm
{
    class Program
    {
        private static bool completed = false;
        private static WebBrowser wb;
        [STAThread]
        static void Main(string[] args)
        {
            wb = new WebBrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            wb.Navigate("http://www.google.com");
            while (!completed)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
            Console.Write("\n\nDone with it!\n\n");
            Console.ReadLine();
        }


        static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Console.WriteLine(wb.Document.Body.InnerHtml);
            completed = true;
        }
    }
}


I know I may receive some bashing over using Application.DoEvents() but it still works though and fullfills your request of not wanting to use a form as a container. System.Windows.Forms still has to be included though as that is where the WebBrowser control lives.

Hint: The STAThread attribute of procedure main is essential when initializing the WebBroswer control as this is not allowed in Multi-Threaded Appartment Mode.

[Edit]There may be some other way than using Application.DoEvents() which would involve implementing and setting up one's own message pump. As time allows I will look into this and report on my findings[/Edit]

Happy coding and have fun!

-MRB
 
Share this answer
 
v3
Comments
Espen Harlinn 16-May-11 15:34pm    
My 5 - nice of you to show that it can be done :)
Manfred Rudolf Bihy 16-May-11 15:40pm    
Thanks Espen!
saishree 12-Mar-13 8:49am    
this code is working. True. But what if i want to search an element on the page opened by the 'wb'. I tried to use 'wb.Document.GetElementById("...")' but it returns null. I have tried opening the web page otherwise and the same element is present. Is there any other work around?
Member 11649326 27-Sep-16 10:25am    
This is a material for another question not this one. Please open another question.
then force your application to use the clients browser with

VB
System.Diagnostics.Process.Start("http://www.website.com")
 
Share this answer
 
The WebBrowser control is just that - a control. It has to be placed in a container (a form) to work. If you don't wanta border or titlebar on the form, you can do that, but you must put the control into a form in order to use it.
 
Share this answer
 
Comments
JeremH 16-May-11 11:02am    
I want to use without form
#realJSOP 16-May-11 13:15pm    
Well, you can't. How many different ways do I have to say that?
Manfred Rudolf Bihy 16-May-11 14:29pm    
I must object! See my solution for an example of a console application that works with a WebBrowser control that is not hosted inside a System.Windows.Form. (It's in C# though as I'm not accustomed to VB.net)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900