views:

739

answers:

3

I wanna use AxWebBrowser on console application, but it give me following exception:

Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.

anybody please help me on this by any sample code for using AxWebBrowser in console application c# without any exeption ...

A: 

Add the STAThread attribute to your Main method.

However, you should not be using the "raw" ActiveX control.

Instead, add a reference to System.Windows.Forms.dll and use the WebBrowser class. (Yes, you can do that in a Console app)


Also, automating IE is not ideal. You should consider using the WebCLient class.

SLaks
Could you please write a sample code for adding STAThread attribue ...
mohsen asfia
Just add `[STAThread]` above your `Main()` method.
Rubens Farias
Thank you but actually I have a class named "Browse" that a console application program calls its "StartBrowse" method with url input parameter and in this function I should browse that url and do some processing and return some results so where should I put [STAThread] attribute? I put it before "StartBrowse" method but it gives me same exception at the run time!!
mohsen asfia
You need to apply it to the `Main` method.
SLaks
+1  A: 

Yes, the [STAThread] attribute is required on your Main() method so that COM is initialized properly to make the main thread a Single Threaded Apartment. That's not all though, you will also need to pump a message loop. That's a requirement for an STA. Without one, WebBrowser cannot update its state or run its event handlers, you'll never get the DocumentCompleted event for example. You can get a message loop with Application.Run().

Your console application is now indistinguishable from a Windows Forms application. It is actually easier to get everything right by starting a new project with the Windows Forms application project template, then Project + Properties, Output type = Console Application. Edit the Application.Run() call in Program.cs so it doesn't create a form. It won't make dealing with Application.Run() any easier, consider a Timer to run code.

Hans Passant
A: 

My class is as below but in the run time it gives me System.Windows.Forms.AxHost+InvalidActiveXStateException:

public class Browse
{

    private static AxWebBrowser wBrowser;         
    public static Result StartBrowse(string url)
    {
        var validUri = (url.Contains("http://") ? url : "http://" + url);
        wBrowser = new AxWebBrowser();

        System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(AxWebBrowser));

        ((ISupportInitialize) (wBrowser)).BeginInit();
        wBrowser.OcxState = ((AxHost.State)(resources.GetObject("wBrowser.OcxState")));

        wBrowser.NewWindow2 += wBrowser_NewWindow2;
        wBrowser.NewWindow3 += wBrowser_NewWindow3;
        wBrowser.DocumentComplete += wBrowser_DocumentComplete;
        wBrowser.DownloadComplete += wBrowser_DownloadComplete;
        if (string.IsNullOrEmpty(html) || validUri != url)
        {
            object empty = System.Reflection.Missing.Value;
            wBrowser.Silent = true;
            wBrowser.Navigate(validUri, ref empty, ref empty, ref empty, ref empty);
        }
        return null;
    }

    static void wBrowser_DownloadComplete(object sender, EventArgs e)
    {
        doAlgorithm();
    }

    static void wBrowser_DocumentComplete(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
    {
        doAlgorithm();
    }

    static void wBrowser_NewWindow3(object sender, DWebBrowserEvents2_NewWindow3Event e)
    {
        e.cancel = true;
    }

    static void wBrowser_NewWindow2(object sender, DWebBrowserEvents2_NewWindow2Event e)
    {
        e.cancel = true;
    }
}
mohsen asfia