How to use WebBrowser in .Net Compact Framework?

G

Goldbach

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
 
P

Peter Foot

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
 
G

Goldbach

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 said:
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 said:
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
 
S

Simon Hart [MVP]

See this this helps:
https://connect.microsoft.com/Visua...Feedback.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 said:
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 said:
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 said:
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
 
G

Goldbach

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 said:
See this this helps:
https://connect.microsoft.com/Visua...Feedback.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 said:
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 said:
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

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
 

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