求救 ATL中实现多线程访问在工作线程中触发事件
int nConnections = m_vec.GetSize(); //当访问到这里后程序一点提示都没有关闭了
搞了一天不知道,到底该怎么改写,希望大侠们帮下
#ifndef _DCC_H_
#define _DCC_H_
#include "ATLCPImplMT.h"template <class T>
class CProxy_IDCWBEvents : public IConnectionPointImplMT<T, &DIID__IDCWBEvents, CComDynamicUnkArray>
{
public: void Fire_OnEditAcx(long wUID)
{
CComVariant varResult;
T* pT = static_cast<T*>(this);
int nConnectionIndex;
CComVariant* pvars = new CComVariant[1];
int nConnections = m_vec.GetSize(); //当访问到这里后程序一点提示都没有关闭了
for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
{
pT->Lock();
CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock(); CComQIPtr< IDispatch, &IID_IDispatch > pDispatch( sp.p ); if (pDispatch.p != NULL)
{
VariantClear(&varResult);
pvars[0] = wUID;
DISPPARAMS disp = { pvars, NULL, 1, 0 };
pDispatch->Invoke(0x47, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
}};
#endif

解决方案 »

  1.   

    int nConnections = m_vec.GetSize();
    为什么我没法使用???都是跑到这里 没一点反应程序就关闭了
    void Fire_OnEditAcx(long wUID)
    {
      int nConnectionIndex;
      CComVariant* pvars = new CComVariant[1];
      int nConnections = m_vec.GetSize();
     
      for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
      {
    CComPtr<IUnknown> sp;
    sp.Attach (GetInterfaceAt(nConnectionIndex));  IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
      if (pDispatch != NULL)
      {
      pvars[0] = wUID;
      DISPPARAMS disp = { pvars, NULL, 1, 0 };
      pDispatch->Invoke(0x47, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, NULL, NULL, NULL);
      }
      }
      delete[] pvars;
                  }
      

  2.   

    int nConnections = m_vec.GetSize();
    为什么我没法使用???都是跑到这里 没一点反应程序就关闭了
    没反应程序就关闭了, 这是程序出了异常, 调试器应该捕捉到, 你看看VC的Outpout窗口, 看有没有异常信息, 针对问题去调试
      

  3.   

    数据都没有任何同步保护,什么情况都有可能发生。你自己也看到了,下面有一个对 m_vec 的访问是加了保护的,前后调用了 pT->Lock() pT->Unlock()在不了解 IConnectionPointImplMT 实现细节的前提下,我建议放弃用这个模板类,改成工作线程向组件线程中的一个隐藏窗口发送消息,由组件线程触发事件,不会有任何同步的麻烦。
      

  4.   

    数据都没有任何同步保护,什么情况都有可能发生。你自己也看到了,下面有一个对 m_vec 的访问是加了保护的,前后调用了 pT->Lock() pT->Unlock()在不了解 IConnectionPointImplMT 实现细节的前提下,我建议放弃用这个模板类,改成工作线程向组件线程中的一个隐藏窗口发送消息,由组件线程触发事件,不会有任何同步的麻烦。
      

  5.   

    我在网络上搜索了很多有关的资料,发现如下,但是根本问题没解决:从这里发现了另外一个在 Alt 中的线程中实现激发事件
    http://topic.csdn.net/t/20030822/18/2178342.html这个路径就刷如何在Alt中实现多线程激发事件
    http://www.codeguru.com/Cpp/COM-Tech/atl/atl/article.php/c75
    下边是我实现的代码,这边错误没出现,但是
    :\TDdownload\ATLCPImplMT.h(232) : error C2440: 'type cast' : cannot convert from 'class ATL::CComPtr<struct IUnknown>' to 'unsigned long'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
            C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xmemory(61) : while compiling class-template member function 'struct IUnknown *__thiscall IConnectionPointImplMT<class CCCWB,&struct _GUID const DIID__IDCWBEvents,class CComDynamicUnkArra错误指在ATLCPImplMT.h代码红色部分
    template <class T, const IID* piid, class CDV>
    LPUNKNOWN IConnectionPointImplMT<T, piid, CDV>::GetInterfaceAt(
    int nConnectionIndex)
    {
    m_CPMTCritSec.Lock(); LPUNKNOWN pUnk = NULL;
    // IConnectionPointImplMT Vector stores DWORDs instead of IUnknown pointers, 
    // explicit cast required:
    ;
    DWORD dwGITCookie = (DWORD)(m_vec.GetAt(nConnectionIndex)); if (dwGITCookie != NULL)
    {
    IID iid;
    GetConnectionInterface(&iid);
    HRESULT hr = m_pGIT->GetInterfaceFromGlobal(
    dwGITCookie, iid, reinterpret_cast<void **>(&pUnk));
    ATLASSERT(hr == S_OK);
    } m_CPMTCritSec.Unlock();  return pUnk;
    }#ifndef _DCC_H_
    #define _DCC_H_
    #include "ATLCPImplMT.h"
    #include "CEventProxy.h"template <class T>
    class CProxy_IDCWBEvents : public IConnectionPointImplMT<T, &DIID__IDCWBEvents, CComDynamicUnkArray_GIT>
    {
    public:    void Fire_OnEditAcx(long wUID)
        {
            CComVariant varResult;
            T* pT = static_cast<T*>(this);
            int nConnectionIndex;
            CComVariant* pvars = new CComVariant[1];
            int nConnections = m_vec.GetSize(); //当访问到这里后程序一点提示都没有关闭了        
            for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
            {
                pT->Lock();
                CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
                pT->Unlock();            CComQIPtr< IDispatch, &IID_IDispatch > pDispatch( sp.p );            if (pDispatch.p != NULL)
                {
                    VariantClear(&varResult);
                    pvars[0] = wUID;
                    DISPPARAMS disp = { pvars, NULL, 1, 0 };
                    pDispatch->Invoke(0x47, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
                }
            }
            delete[] pvars;
        }};
    #endif
      

  6.   

    希望有大侠能帮下 解决 ATLCPImplMT.h代码红色部分错误template <class T, const IID* piid, class CDV>
    LPUNKNOWN IConnectionPointImplMT<T, piid, CDV>::GetInterfaceAt(
    int nConnectionIndex)
    {
    m_CPMTCritSec.Lock();LPUNKNOWN pUnk = NULL;
    // IConnectionPointImplMT Vector stores DWORDs instead of IUnknown pointers,  
    // explicit cast required:
    ;
    DWORD dwGITCookie = (DWORD)(m_vec.GetAt(nConnectionIndex)); if (dwGITCookie != NULL)
    {
    IID iid;
    GetConnectionInterface(&iid);
    HRESULT hr = m_pGIT->GetInterfaceFromGlobal(
    dwGITCookie, iid, reinterpret_cast<void **>(&pUnk));
    ATLASSERT(hr == S_OK);
    }m_CPMTCritSec.Unlock();  return pUnk;
    }
      

  7.   

    发现刷引用过多,呵呵,但是出现了非常多的错误
    #ifndef _VBMHWBCP_H_
    #define _VBMHWBCP_H_
    //#include "ATLCPImplMT.h"  应该注释掉
    #include "CEventProxy.h"
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    EventSink.cpp
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    vbMHWB.cpp
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    vbWB.cpp
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vncc.h(1981) : see reference to class template instantiation 'CProxy_IDCWBEvents<T>' being compiled
    F:\TDdownload\vncc.h(7) : error C2504: 'IConnectionPointImplMT' : base class undefined
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2143: syntax error : missing ',' before '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    F:\TDdownload\vncc.h(7) : error C2059: syntax error : '<'
            F:\TDdownload\vbMHWB\vbWB.h(565) : see reference to class template instantiation 'CProxy_IDCWBEvents<class TcWB>' being compiled
    Generating Code...
    Error executing cl.exe.
    Creating browse info file...
      

  8.   

    怎么解决的,我也遇到该问题了,[email protected],多谢