Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Fireing events from worker thread

Fireing events from worker thread

Scheduled Pinned Locked Moved ATL / WTL / STL
c++comdebugginghelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sivakumar R
    wrote on last edited by
    #1

    Hi, I have written a dll using ATL. In which I want to fire events from a call back function. When I tried to do that my program crashes. So that I have implemented a hidden window (which I saw in an article in this site) and posting the message to that window. From there I fire the events. It is working quite fine. But my problem is, when I closes the application which used my COM object, it crashes at the end. I cannot trace what is the problem. I describe here step by step what I have written in my program.. 1. This is the hidden window i have created.. // mywnd.h : header file // ///////////////////////////////////////////////////////////////////////////// // CMyWindow window class CClsRecord; class CMyWindow : public CWnd { // Construction public: LRESULT OnFireEventForThread(WPARAM wParam, LPARAM lParam); CClsRecord *m_pControl; HWND Create(); CMyWindow(CClsRecord *pCtrl); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyWindow) protected: virtual void PostNcDestroy(); //}}AFX_VIRTUAL // Implementation public: virtual ~CMyWindow(); // Generated message map functions protected: //{{AFX_MSG(CMyWindow) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// HWND CMyWindow::Create() { //Register a window class LPCTSTR classname = 0; classname = AfxRegisterWndClass(0); //Create the window and return it's handle CWnd::CreateEx(NULL,classname,NULL,NULL,1,1,1,1,NULL,NULL); ASSERT(m_hWnd!=NULL); return m_hWnd; } LRESULT CMyWindow::OnFireEventForThread(WPARAM wParam, LPARAM lParam) { CComBSTR bstr; //This is a one-message-does-everything handler. If wParam is not //set, that means our message has been sent to fire the even. //If wParam is set that means we're being asked to destroy ourselves if(!wParam) { if (lParam!=NULL) { bstr = m_pControl->GetEventMessage(); m_pControl->Fire_OnEvent(lParam,bstr.Copy()); } } else if(wParam==1) DestroyWindow(); return TRUE; } void CMyWindow::PostNcDestroy() { //clean up this object delete this; } 2. In the ClsRecord class, I have created a thread like this CMyWindow *pWnd = new CMyWindow(this); HWND hwndTarget = pWnd->Create(); //create a thread for handling all the events HANDLE threadHandle = CreateThread

    E 1 Reply Last reply
    0
    • S Sivakumar R

      Hi, I have written a dll using ATL. In which I want to fire events from a call back function. When I tried to do that my program crashes. So that I have implemented a hidden window (which I saw in an article in this site) and posting the message to that window. From there I fire the events. It is working quite fine. But my problem is, when I closes the application which used my COM object, it crashes at the end. I cannot trace what is the problem. I describe here step by step what I have written in my program.. 1. This is the hidden window i have created.. // mywnd.h : header file // ///////////////////////////////////////////////////////////////////////////// // CMyWindow window class CClsRecord; class CMyWindow : public CWnd { // Construction public: LRESULT OnFireEventForThread(WPARAM wParam, LPARAM lParam); CClsRecord *m_pControl; HWND Create(); CMyWindow(CClsRecord *pCtrl); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyWindow) protected: virtual void PostNcDestroy(); //}}AFX_VIRTUAL // Implementation public: virtual ~CMyWindow(); // Generated message map functions protected: //{{AFX_MSG(CMyWindow) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// HWND CMyWindow::Create() { //Register a window class LPCTSTR classname = 0; classname = AfxRegisterWndClass(0); //Create the window and return it's handle CWnd::CreateEx(NULL,classname,NULL,NULL,1,1,1,1,NULL,NULL); ASSERT(m_hWnd!=NULL); return m_hWnd; } LRESULT CMyWindow::OnFireEventForThread(WPARAM wParam, LPARAM lParam) { CComBSTR bstr; //This is a one-message-does-everything handler. If wParam is not //set, that means our message has been sent to fire the even. //If wParam is set that means we're being asked to destroy ourselves if(!wParam) { if (lParam!=NULL) { bstr = m_pControl->GetEventMessage(); m_pControl->Fire_OnEvent(lParam,bstr.Copy()); } } else if(wParam==1) DestroyWindow(); return TRUE; } void CMyWindow::PostNcDestroy() { //clean up this object delete this; } 2. In the ClsRecord class, I have created a thread like this CMyWindow *pWnd = new CMyWindow(this); HWND hwndTarget = pWnd->Create(); //create a thread for handling all the events HANDLE threadHandle = CreateThread

      E Offline
      E Offline
      Ernest Laurentin
      wrote on last edited by
      #2

      Did you call CoInitializeEx(...) in both threads where you are accessing your object? Don't forget to call also CoUninitialize() before the thread exits. ÿFor the bread of God is he who comes down from heaven and gives life to the world. - John 6:33

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups