vendredi 24 juin 2016

PostThreadMessage C++ MFC (CWinThread)

I am using MFC C++, and I'm created a worker thread to handle ethernet connections on my program. I am trying to send a user-defined message from the main window to the worker thread using PostThreadMessage but nothing happens, the message handler isnt handling the message. Any ideas?

EDIT: Added start() and run(). Start calls run

Thanks in advance!

    class TwentyHzClass : public CWinThread
    {
        public:
    static UINT start(LPVOID param);

    void run();
    afx_msg void OnThreadMessage(WPARAM wParam, LPARAM lParam);
        //
    protected:
        DECLARE_MESSAGE_MAP()
    }

The call from main:

m_twentyHzParam = new TwentyHzClass::THREADSTRUCT;
        m_twentyHzParam->fnPtr = (TwentyHzClass*)&m_twentyHz;
        m_twentyHzParam->pDC = m_DC;


        test = AfxBeginThread(TwentyHzClass::start, m_twentyHzParam);
        test->PostThreadMessage(WM_TWENTYHZ_STOP, 0, 0);
        TRACE("ThreadID = %dn", test->m_nThreadID);

Then on my WorkerThread

BEGIN_MESSAGE_MAP(TwentyHzClass, CWinThread)
    ON_THREAD_MESSAGE(WM_TWENTYHZ_STOP, OnThreadMessage)
END_MESSAGE_MAP()

UINT TwentyHzClass::start(LPVOID param)
{
   THREADSTRUCT* ts = (THREADSTRUCT*) param;

   ts->fnPtr->m_pDC = ts->pDC;
   ts->fnPtr->run();

   return 0;
}

void TwentyHzClass::run()
{
  ...
  SetWaitableTimer(hTimer, &liDueTime, PERIODIC_RATE, NULL, NULL, 0);
  while (true)
  {
    WaitForSingleObject(hTimer, INFINITE);
    .. do stuff..
  }
}

afx_msg void TwentyHzClass::OnThreadMessage(WPARAM wParam, LPARAM lParam)
{
    OutputDebugString("RECEIVED THREAD MESSAGE");
}

Aucun commentaire:

Enregistrer un commentaire