怎么解决?当前线程不在单线程单元中,因此无法实例化 ActiveX 控件

hongjunmei123456 2009-08-22 09:56:09
我想在主窗体中单击某按钮,出现一个子窗体,并且在子窗体上生成一条曲线,曲线的生成采用第三方控件。

在主窗体FORM1中有以下代码:
private void toolStripButton5_Click(object sender, EventArgs e)
{
frmquxiannihe fq = new frmquxiannihe(); //打开子窗体.
fq.ShowDialog();
}

子窗体frmquxiannihe中有一个第三方控件axcwgraph,生成曲线的。
我单击主窗体上的按钮时,运行到“this.axCWGraph1 = new AxCWUIControlsLib.AxCWGraph();”时出现“当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“b68dbfab-16a0-11ce-80bf-0020af31cef9”。main 上已经有[STAThread] 了。
[STAThread]
static void Main()
{




...全文
546 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongjunmei123456 2009-08-29
  • 打赏
  • 举报
回复
我另外新建一个项目试了一下,这个控件可以用的。在这个项目里出错了。C#刚学的,不懂怎么解决。
hongjunmei123456 2009-08-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dancingbit 的回复:]
真是晕...

1.this.axCWGraph1 = new AxCWUIControlsLib.AxCWGraph();这条语句在哪个方法中?
2.主窗体是如何调用这个方法的?
[/Quote]
这是代码:
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmquxiannihe));
this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.axCWGraph1 = new AxCWUIControlsLib.AxCWGraph();
this.toolStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axCWGraph1)).BeginInit();
this.SuspendLayout();


我留个QQ号码:393295204,你加我吧。
dancingbit 2009-08-25
  • 打赏
  • 举报
回复
真是晕...

1.this.axCWGraph1 = new AxCWUIControlsLib.AxCWGraph();这条语句在哪个方法中?
2.主窗体是如何调用这个方法的?
hongjunmei123456 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 dancingbit 的回复:]
我知道你生成曲线的控件是在子窗体中,但按照你的描述,你是在单击主窗体的按钮之后,由子窗体上的控件生成曲线的,这个地方很可能有跨线程的操作,明白?
[/Quote]
嗯,就是这样,所以运行到“this.axCWGraph1 = new AxCWUIControlsLib.AxCWGraph();”时出现“当前线程不在单线程单元中,因此无法实例化 ActiveX 控件”错误,我不知道怎么解决。麻烦你教教我好吗?
dancingbit 2009-08-23
  • 打赏
  • 举报
回复
我知道你生成曲线的控件是在子窗体中,但按照你的描述,你是在单击主窗体的按钮之后,由子窗体上的控件生成曲线的,这个地方很可能有跨线程的操作,明白?
hongjunmei123456 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dancingbit 的回复:]
这个只是显示子窗体,显示曲线的呢?
[/Quote]

生成曲线的控件在子窗体里。
dancingbit 2009-08-22
  • 打赏
  • 举报
回复
这个只是显示子窗体,显示曲线的呢?
hongjunmei123456 2009-08-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yzp_001688 的回复:]
是OCX的吧。那样应该可以,我做过。
[/Quote]
可以说的明白点吗?谢谢啦。
雨辰DIY科创 2009-08-22
  • 打赏
  • 举报
回复
是OCX的吧。那样应该可以,我做过。
hongjunmei123456 2009-08-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yzp_001688 的回复:]
弄个USER控件,把它的声明弄成PUBLIC的看看,然后引用你的USER控件。
[/Quote]
可以说的详细点吗?我是刚学C#的,许多地方很不明白啊。
雨辰DIY科创 2009-08-22
  • 打赏
  • 举报
回复
http://china.alibaba.com/company/detail/contact/yzp100.htm 啊我的小店。做个广告大家支持啊。

弄个USER控件,把它的声明弄成PUBLIC的看看,然后引用你的USER控件。
雨辰DIY科创 2009-08-22
  • 打赏
  • 举报
回复
弄个USER控件,把它的声明弄成PUBLIC的看看,然后引用你的USER控件。
hongjunmei123456 2009-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dancingbit 的回复:]
单击主窗体上的按钮,在子窗体上生成曲线?
这是通过什么方式完成的?
[/Quote]
就是下面的click事件。
private void toolStripButton5_Click(object sender, EventArgs e)
{
frmquxiannihe fq = new frmquxiannihe(); //打开子窗体.
fq.ShowDialog();
}
dancingbit 2009-08-22
  • 打赏
  • 举报
回复
单击主窗体上的按钮,在子窗体上生成曲线?
这是通过什么方式完成的?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧