我有一个Windows Service,启动的时候会调用 ClassLibrary1.Form1,Form1上有一个ActiveX。
服务使用帐号 Local System,而且允许服务与桌面交互。
但是发生错误,该怎么解决啊:System.Threading.ThreadStateException: 因为当前线程不在单线程单元中,故无法实例化 ActiveX 控件“b0b25446-7656-456c-8d3c-60a0ac6eac49”。
   at System.Windows.Forms.AxHost..ctor(String clsid, Int32 flags)
   at System.Windows.Forms.AxHost..ctor(String clsid)
   at AxJDSMSCtrl.AxJindiSMSControl..ctor()
   at ClassLibrary1.Form1.InitializeComponent()
   at ClassLibrary1.Form1..ctor()
   at WindowsService1.Service1.OnStart(String[] args)

解决方案 »

  1.   

    你应该把form做成一个单独的app,然后用它与service进行交互,做在service中是一个不明智的做法。
      

  2.   

    即使不用 Form,我的 Service 也会出错。
    这个 Service 要求串口通讯,而这个功能是一个第三方的 ActiveX 提供的,
    所以 Service 必须调用这个 OCX
      

  3.   

    to 这个 Service 要求串口通讯,而这个功能是一个第三方的 ActiveX 提供的,建议你用service改成app。
      

  4.   

    o, 换句话说也就是 这个用 Windows Service 无法实现喽?
      

  5.   

    Check following things on production server 
    1. On What login credentials this service is running ? 
    2. This user has sufficient previlages to access resources , if  not try changing the user accounts? 
    3. Is this service making calls to other assemblies dynamically or other COM Dlls dynamically if so make sure these assemblies exist at proper path and Com Dlls are registered. 
    4 OnStart has to terminate in 30 seconds or service manager shuts down the service. It's possible that CLR cannot be loaded up timely. 
      

  6.   

    jiangsheng(蒋晟.Net[MVP]) 您说什么呢?我也用Administrator的帐号试过了,同样的错误,
    "因为当前线程不在单线程单元中,故无法实例化 ActiveX 控件"
    我是个developer,上述原因都不可能存在因为同样的程序 WinForm 就能运行
      

  7.   

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