WindowsForm有效,但是WebForm存在单线程单元问题, [英] WindowsForm works, but WebForm has single-threaded apartment problem,

查看:68
本文介绍了WindowsForm有效,但是WebForm存在单线程单元问题,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ASP.NET Web项目中实例化此对象,但出现此错误:

I''m trying to instantiate this object in a ASP.NET web project and i got this error:

<br />
ActiveX control ''05e8280c-d45a-494f-ae42-840a40444aff'' cannot be instantiated because the current thread is not in a single-threaded apartment.



我页面的代码:



My page''s code:

public partial class _Default : System.Web.UI.Page
{
   protected void InitializeComponent()
    {
       AxBIOPLUGINACTXLib.AxBioPlugInActX fReader = new AxBIOPLUGINACTXLib.AxBioPlugInActX();
    }
}

推荐答案

您的ActiveX对象要求您的线程是STA(单线程单元).但是asp.net服务器在多线程单元状态下生成线程以处理请求

解决方法是创建STA线程并在其中使用控件.

例如:
(using System.Threading)

Your ActiveX object requires your thread to be an STA (single thread apartment). But the asp.net server spawns threads for processing requests in a Multithreaded apartment state

Your workaround is to create an STA thread and use the control in it.

Eg:
(using System.Threading)

Thread t = new Thread(ThreadStartMethod);
t.SetApartmentState(ApartmentState.STA);
t.Start();



有关创建&的更多信息,请参见msdn.使用线程



Please see the msdn for more info on creating & using threads


奇怪.您是在新线程(您的线程启动方法)中而不是在新线程之外创建对象的吗?

反正
您可以通过将页面的ASPCompat属性设置为true来打开经典的ASP兼容模式.

Strange. Did you create the object in the new thread (your thread start method) and not outside it?

Anyway,
You can turn on the classic ASP compatibility mode by setting the page''s ASPCompat attribute to true

<%@ Page AspCompat="true" %>


(并且忘记创建和使用另一个线程)

在不使用旧的COM组件或使用在MTA(多线程单元)上运行的组件的页面上,也建议不要打开它.

我很确定这应该可行.让我知道它是否有效.


(and forget about creating & using another thread)

It''s also advisable that you do not turn it on, on pages where you are not using old COM components or where you are using components running on MTA (Multi-thread apartment).

I''m pretty sure this should work. Let me know if it works or not.


这篇关于WindowsForm有效,但是WebForm存在单线程单元问题,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆