Webbrowser in compact framework

649 views Asked by At

I would like to use a WebBrowser component in a .NET Compact Framework 3.5 project. I am confronted with contradictive information about this.

If I just try to use it, I get the following exception:

System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
at System.Windows.Forms.WebBrowser..ctor()
at dce.pd.dialogmgr.baseimpl.DialogDeviceImpl.createGui(XmlElement pDialogManagerNode) in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\baseimpl\DialogDeviceImpl.cs:line 362
at dce.pd.dialogmgr.baseimpl.DialogDeviceImpl.init() in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\baseimpl\DialogDeviceImpl.cs:line 118
at dce.pd.dialogmgr.thinimpl.ThinClient.createDeviceImpl() in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\thinimpl\ThinClient.cs:line 82
at dce.pd.dialogmgr.thinimpl.ThinClient.init() in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\thinimpl\ThinClient.cs:line 87
at dce.pd.dialogmgr.thinimpl.ThinClient.form_Load(Object sender, EventArgs e) in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\thinimpl\ThinClient.cs:line 66
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

To me, this sounds like I have to put the [STAThread] directive to my Main method.

[STAThread]
static void Main()
{
  ...
}

But that doesn't work neither, because the [STAThread] option is not available in compact framework.. So, the application won't even compile now.

Nevertheless, according to Microsoft's documentation, the WebBrowser components is available for compact framework project. There are even some examples. (Unfortunately the full code is not provided).

So, what am I missing ?

1

There are 1 answers

2
bvdb On BEST ANSWER

I finally found what's wrong here.

In fact, the exception above (i.e. System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.) only shows up when I try to run the compiled executable directly on my laptop.

The simulator wasn't working for me. And I finally got the simulator working now. I realized that I had to select an "emulator" device, the kind that ends with "emulator" in its name. (e.g. "Windows Mobile 6 Emulator"). You need to connect these devices before testing, using the "connect to device" button in the toolbar. Once the connection is made, the application actually runs just fine on the emulator.

The question remains: why doesn't the application run directly on my laptop while it does run fine in an emulator. But that's a different question.

Of course the operating system is different. But I have the impression, that the system uses a different version of the .NET framework. Previously I wrongfully assumed that all compact framework apps could also run on a laptop with the full blown .net framework. Now it appears that this is incorrect.