场景:
    第三方COM组件不支持跨线程调用。
    应用程序启动需要启动:
        1.UI
        2.HttpServer
    启动HTTPServer的时候会启动第三方的组件    UI访问HttpServer,HttpServer调用第三方接口然后response。问题:
    由于第三方组件不支持跨线程调用,导致程序卡死。
异常:
    The CLR has been unable to transition from COM context 0x4ee0f8 to COM context 0x4ee268 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.请提供dispatch的例子,谢谢

解决方案 »

  1.   


    不行,因为COM组件的创建者不是COM组件的调用者,肯定要跨线程调用的
      

  2.   

    那看看是否实现了IDispatch,实现了的话可以Inovoke
      

  3.   

    创建一个与那个com线程模型兼容的线程
      

  4.   

    HttpServer 用 STAAttribute 启动如何?
      

  5.   

    HTTPServer是自定的server,在主程序启动的,HTTPServer是第三方的开源webservice
      

  6.   

    有个例子,就是在WebService 里启 WebBrowser(底层是COM)这东东就需要STA。我当时是试着玩,没有实际价值。访问的时候需要所有客户端Request同步。
      

  7.   

    终于翻出那段代码了关键部分:public void Run()
    {
        this._thread = new Thread(Login);
        this._thread.SetApartmentState(ApartmentState.STA);
        this._thread.Start();
    }
      

  8.   

    线程不行就用进程试试。
    win32 的COM是对线程的支持有点问题,但是对进程支持还不错。
    如果没有办法解决这个线程问题,那么你就得改改你的设计思路了。