I want to use WebBrowser in .Net Compact Framework,
but following exception occurred during runtime (running on Windows XP Pro)

---------------------------
[UserControl1.cs] UserControl1() failed!

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 WebTest.UserControl1..ctor()
---------------------------

In .Net Framework, it can set [STAThread] to avoid this exception,
but it only supports [MTAThread] in .Net Compact Framework.
How can I resolved it?
Thanks.

ps. My platform is Windows CE 5.0

--
Regards,
Goldbach

RE: How to use WebBrowser in .Net Compact Framework? by Goldbach

Goldbach
Mon Jul 14 07:20:00 CDT 2008

Post my code,

public Form1()
{
InitializeComponent();
this.webBrowser1.Navigate(new Uri("http://www.msn.com/"));
}

Re: How to use WebBrowser in .Net Compact Framework? by Peter

Peter
Mon Jul 14 07:58:09 CDT 2008

The error indicates an issue within a user control and your code snippet
indicates a webbrowser hosted directly within a form. Can you explain
exactly what you are doing?

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - Software Solutions for a Mobile World
In The Hand Ltd - .NET Components for Mobility

"Goldbach" <Goldbach@discussions.microsoft.com> wrote in message
news:72F22935-FB28-429F-A71E-5A1990B4A115@microsoft.com...
>I want to use WebBrowser in .Net Compact Framework,
> but following exception occurred during runtime (running on Windows XP
> Pro)
>
> ---------------------------
> [UserControl1.cs] UserControl1() failed!
>
> 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 WebTest.UserControl1..ctor()
> ---------------------------
>
> In .Net Framework, it can set [STAThread] to avoid this exception,
> but it only supports [MTAThread] in .Net Compact Framework.
> How can I resolved it?
> Thanks.
>
> ps. My platform is Windows CE 5.0
>
> --
> Regards,
> Goldbach


Re: How to use WebBrowser in .Net Compact Framework? by Goldbach

Goldbach
Mon Jul 14 09:26:07 CDT 2008

Thanks for your reply.
The develope environment is VS2005, and the following is my code,

[Form1.cs]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WebBrowserTest_CE50
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

this.webBrowser1.Navigate(new Uri("http://tw.yahoo.com/"));
}
}
}


[Form1.Designer.cs]
namespace WebBrowserTest_CE50
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.MainMenu mainMenu1;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(638, 455);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(638, 455);
this.Controls.Add(this.webBrowser1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.WebBrowser webBrowser1;
}
}


[Program.cs]
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WebBrowserTest_CE50
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}

--
Regards,
Goldbach


"Peter Foot" wrote:

> The error indicates an issue within a user control and your code snippet
> indicates a webbrowser hosted directly within a form. Can you explain
> exactly what you are doing?
>
> Peter
>
> --
> Peter Foot
> Microsoft Device Application Development MVP
> peterfoot.net | appamundi.com | inthehand.com
> APPA Mundi Ltd - Software Solutions for a Mobile World
> In The Hand Ltd - .NET Components for Mobility
>
> "Goldbach" <Goldbach@discussions.microsoft.com> wrote in message
> news:72F22935-FB28-429F-A71E-5A1990B4A115@microsoft.com...
> >I want to use WebBrowser in .Net Compact Framework,
> > but following exception occurred during runtime (running on Windows XP
> > Pro)
> >
> > ---------------------------
> > [UserControl1.cs] UserControl1() failed!
> >
> > 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 WebTest.UserControl1..ctor()
> > ---------------------------
> >
> > In .Net Framework, it can set [STAThread] to avoid this exception,
> > but it only supports [MTAThread] in .Net Compact Framework.
> > How can I resolved it?
> > Thanks.
> >
> > ps. My platform is Windows CE 5.0
> >
> > --
> > Regards,
> > Goldbach
>
>

Re: How to use WebBrowser in .Net Compact Framework? by srhartone

srhartone
Mon Jul 14 11:36:01 CDT 2008

See this this helps:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=167101&wa=wsignin1.0

It comes down to the fact that under COM, single threaded apartment threads
are not supported on CF.

You could try using a conditional compiler directive if you want to share
your source code for both desktop and device whereby you change the threading
model under desktop to STA.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"Goldbach" wrote:

> Thanks for your reply.
> The develope environment is VS2005, and the following is my code,
>
> [Form1.cs]
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
>
> namespace WebBrowserTest_CE50
> {
> public partial class Form1 : Form
> {
> public Form1()
> {
> InitializeComponent();
>
> this.webBrowser1.Navigate(new Uri("http://tw.yahoo.com/"));
> }
> }
> }
>
>
> [Form1.Designer.cs]
> namespace WebBrowserTest_CE50
> {
> partial class Form1
> {
> /// <summary>
> /// Required designer variable.
> /// </summary>
> private System.ComponentModel.IContainer components = null;
> private System.Windows.Forms.MainMenu mainMenu1;
>
> /// <summary>
> /// Clean up any resources being used.
> /// </summary>
> /// <param name="disposing">true if managed resources should be
> disposed; otherwise, false.</param>
> protected override void Dispose(bool disposing)
> {
> if (disposing && (components != null))
> {
> components.Dispose();
> }
> base.Dispose(disposing);
> }
>
> #region Windows Form Designer generated code
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.mainMenu1 = new System.Windows.Forms.MainMenu();
> this.webBrowser1 = new System.Windows.Forms.WebBrowser();
> this.SuspendLayout();
> //
> // webBrowser1
> //
> this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
> this.webBrowser1.Location = new System.Drawing.Point(0, 0);
> this.webBrowser1.Name = "webBrowser1";
> this.webBrowser1.Size = new System.Drawing.Size(638, 455);
> //
> // Form1
> //
> this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
> this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
> this.AutoScroll = true;
> this.ClientSize = new System.Drawing.Size(638, 455);
> this.Controls.Add(this.webBrowser1);
> this.Menu = this.mainMenu1;
> this.Name = "Form1";
> this.Text = "Form1";
> this.ResumeLayout(false);
>
> }
>
> #endregion
>
> private System.Windows.Forms.WebBrowser webBrowser1;
> }
> }
>
>
> [Program.cs]
> using System;
> using System.Collections.Generic;
> using System.Windows.Forms;
>
> namespace WebBrowserTest_CE50
> {
> static class Program
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [MTAThread]
> static void Main()
> {
> Application.Run(new Form1());
> }
> }
> }
>
> --
> Regards,
> Goldbach
>
>
> "Peter Foot" wrote:
>
> > The error indicates an issue within a user control and your code snippet
> > indicates a webbrowser hosted directly within a form. Can you explain
> > exactly what you are doing?
> >
> > Peter
> >
> > --
> > Peter Foot
> > Microsoft Device Application Development MVP
> > peterfoot.net | appamundi.com | inthehand.com
> > APPA Mundi Ltd - Software Solutions for a Mobile World
> > In The Hand Ltd - .NET Components for Mobility
> >
> > "Goldbach" <Goldbach@discussions.microsoft.com> wrote in message
> > news:72F22935-FB28-429F-A71E-5A1990B4A115@microsoft.com...
> > >I want to use WebBrowser in .Net Compact Framework,
> > > but following exception occurred during runtime (running on Windows XP
> > > Pro)
> > >
> > > ---------------------------
> > > [UserControl1.cs] UserControl1() failed!
> > >
> > > 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 WebTest.UserControl1..ctor()
> > > ---------------------------
> > >
> > > In .Net Framework, it can set [STAThread] to avoid this exception,
> > > but it only supports [MTAThread] in .Net Compact Framework.
> > > How can I resolved it?
> > > Thanks.
> > >
> > > ps. My platform is Windows CE 5.0
> > >
> > > --
> > > Regards,
> > > Goldbach
> >
> >

Re: How to use WebBrowser in .Net Compact Framework? by Goldbach

Goldbach
Tue Jul 22 00:48:04 CDT 2008

I want to use the same application to run on both desktop and device,
but I don't know how to use the conditional compiler directive.
Could you tell me the detail configuration?

Thanks~
--
Regards,
Goldbach


"Simon Hart [MVP]" wrote:

> See this this helps:
> https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=167101&wa=wsignin1.0
>
> It comes down to the fact that under COM, single threaded apartment threads
> are not supported on CF.
>
> You could try using a conditional compiler directive if you want to share
> your source code for both desktop and device whereby you change the threading
> model under desktop to STA.
> --
> Simon Hart
> Visual Developer - Device Application Development MVP
> http://simonrhart.blogspot.com
>
>
> "Goldbach" wrote:
>
> > Thanks for your reply.
> > The develope environment is VS2005, and the following is my code,
> >
> > [Form1.cs]
> > using System;
> > using System.Collections.Generic;
> > using System.ComponentModel;
> > using System.Data;
> > using System.Drawing;
> > using System.Text;
> > using System.Windows.Forms;
> >
> > namespace WebBrowserTest_CE50
> > {
> > public partial class Form1 : Form
> > {
> > public Form1()
> > {
> > InitializeComponent();
> >
> > this.webBrowser1.Navigate(new Uri("http://tw.yahoo.com/"));
> > }
> > }
> > }
> >
> >
> > [Form1.Designer.cs]
> > namespace WebBrowserTest_CE50
> > {
> > partial class Form1
> > {
> > /// <summary>
> > /// Required designer variable.
> > /// </summary>
> > private System.ComponentModel.IContainer components = null;
> > private System.Windows.Forms.MainMenu mainMenu1;
> >
> > /// <summary>
> > /// Clean up any resources being used.
> > /// </summary>
> > /// <param name="disposing">true if managed resources should be
> > disposed; otherwise, false.</param>
> > protected override void Dispose(bool disposing)
> > {
> > if (disposing && (components != null))
> > {
> > components.Dispose();
> > }
> > base.Dispose(disposing);
> > }
> >
> > #region Windows Form Designer generated code
> >
> > /// <summary>
> > /// Required method for Designer support - do not modify
> > /// the contents of this method with the code editor.
> > /// </summary>
> > private void InitializeComponent()
> > {
> > this.mainMenu1 = new System.Windows.Forms.MainMenu();
> > this.webBrowser1 = new System.Windows.Forms.WebBrowser();
> > this.SuspendLayout();
> > //
> > // webBrowser1
> > //
> > this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
> > this.webBrowser1.Location = new System.Drawing.Point(0, 0);
> > this.webBrowser1.Name = "webBrowser1";
> > this.webBrowser1.Size = new System.Drawing.Size(638, 455);
> > //
> > // Form1
> > //
> > this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
> > this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
> > this.AutoScroll = true;
> > this.ClientSize = new System.Drawing.Size(638, 455);
> > this.Controls.Add(this.webBrowser1);
> > this.Menu = this.mainMenu1;
> > this.Name = "Form1";
> > this.Text = "Form1";
> > this.ResumeLayout(false);
> >
> > }
> >
> > #endregion
> >
> > private System.Windows.Forms.WebBrowser webBrowser1;
> > }
> > }
> >
> >
> > [Program.cs]
> > using System;
> > using System.Collections.Generic;
> > using System.Windows.Forms;
> >
> > namespace WebBrowserTest_CE50
> > {
> > static class Program
> > {
> > /// <summary>
> > /// The main entry point for the application.
> > /// </summary>
> > [MTAThread]
> > static void Main()
> > {
> > Application.Run(new Form1());
> > }
> > }
> > }
> >
> > --
> > Regards,
> > Goldbach
> >
> >
> > "Peter Foot" wrote:
> >
> > > The error indicates an issue within a user control and your code snippet
> > > indicates a webbrowser hosted directly within a form. Can you explain
> > > exactly what you are doing?
> > >
> > > Peter
> > >
> > > --
> > > Peter Foot
> > > Microsoft Device Application Development MVP
> > > peterfoot.net | appamundi.com | inthehand.com
> > > APPA Mundi Ltd - Software Solutions for a Mobile World
> > > In The Hand Ltd - .NET Components for Mobility
> > >
> > > "Goldbach" <Goldbach@discussions.microsoft.com> wrote in message
> > > news:72F22935-FB28-429F-A71E-5A1990B4A115@microsoft.com...
> > > >I want to use WebBrowser in .Net Compact Framework,
> > > > but following exception occurred during runtime (running on Windows XP
> > > > Pro)
> > > >
> > > > ---------------------------
> > > > [UserControl1.cs] UserControl1() failed!
> > > >
> > > > 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 WebTest.UserControl1..ctor()
> > > > ---------------------------
> > > >
> > > > In .Net Framework, it can set [STAThread] to avoid this exception,
> > > > but it only supports [MTAThread] in .Net Compact Framework.
> > > > How can I resolved it?
> > > > Thanks.
> > > >
> > > > ps. My platform is Windows CE 5.0
> > > >
> > > > --
> > > > Regards,
> > > > Goldbach
> > >
> > >