• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 835
  • Last Modified:

PostThreadMessage question

I need to call a notification function from worker thread, in such a maner that it will run from the main thread. I figured that PostThreadMessage would be the best way. For some reason, PostThreadMessage returns 0, and the function is not being called. Here's my code:

class CDll : public CWinApp
      void SyncExecute(void*, LONG);
      void AsyncExecute(void*, void*);
      BOOL InitInstance();
      BOOL ExitInstance();
            // NOTE - the ClassWizard will add and remove member functions here.
            //    DO NOT EDIT what you see in these blocks of generated code !
      afx_msg void OnSyncExecute(WPARAM wParam, LPARAM lParam);

typedef void(NPROC)(LONG);

            // NOTE - the ClassWizard will add and remove mapping macros here.
            //    DO NOT EDIT what you see in these blocks of generated code!
            ON_THREAD_MESSAGE(1272, OnSyncExecute)

//Adds a function from the main thread to execution queue. Used in order to call notification functions from a worker thread
CDll::SyncExecute(void *address, LONG param)
      PostThreadMessage(1272, (WPARAM)address, (LPARAM)param);
      int n=GetLastError();

//Actualy executes the function
CDll::OnSyncExecute(WPARAM wParam, LPARAM lParam)
      NPROC *address = (NPROC*)wParam;

Thanks in advance

1 Solution
Probably for some reason your mainthread does not have a message queue(this is possible if your apllication is a DLL).

Try first to create another object which inherits CWinThread, and create another thread using AfxCreateThread.
Then post your message to that thread.

If this helps then the problem is that your main thread does not have a message loop.

If this does not solve the problem, try calling GetLastError to see what is the error after PostThreadMessage
You main thread derive from CWinapp ,so I think you have a message-loop in mainthread.
If you post message from worker thread,you'd specify the correct thread object in your worker thread,i.e:

AfxGetApp( )->Postthreadmessage(....);

(I'm not a qualified MFC expert,if wrong,please correct me:)
No need to correct Wyn's comment ... CWinThread::PostThreadMessage() sends the message to the thread represantated by the CWinThread object used with the call.
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

The fact that the application has an object derived from CWinApp does not mean that it has a message loop
MikeP090797Author Commented:
The application is a DLL, forgot to mention that. GetLastError returns 0. How do I create a message queue for the thread?

As far as your application is a DLL it does not have a main thread - a DLL is called from the thread of the main program.

So if you want to have a separate thread where to do some work, create an object, which is derived from CWinThread. Use AfxBeginThread, to run a thread and pass your class to the function.

In tth above class add the necessaty macros and functions to handle your message.
MikeP090797Author Commented:
I already have that. AsyncExecute() creates a new worker thread. That works fine. Now, from that worker thread, I need to cause an execution of another function, but in such a manner that it will run from the main thread, and not the worker. If there is another way besides PostThreadMessage, I'm listening..

MikeP, there is NO main thread when you have a DLL. The Application has threads. The DLL only contains functions which Application calls. So what do you mean by main thread?

The only solution is to create a thread, and use this thread to execute all things you want to execute in main thread.

Of course you can modify your application which will use your DLL to have a message pump and to hande your message , but I don't think this is a good idea.
MikeP090797Author Commented:
By main thread I mean the application thread. Here is how things work:
An application calls an exported function in a dll. That function starts a thread. When the thread completes, I want it to call another function in the dll, but as if it was called from the application thread, and not the worker. I guess you can call it a thread context.
Although it is possible to do this, there are a lot of limitations.

1. When you start the second thread, you must start retrieving messages from the main thread's message loop, and wait for the message from the second thread. However this will block the main thread and will not return to the caller until the second thread finishes. It seems that this is not what you want.

2. The second way to do this is to modify the application's message loop to look for messages from the second thread and to call a function in the DLL whenever recieves such message. However this solution requires every application which uses this dll to handle the message from the second thread

3. Another way to do this is if you are sure that the main application has a window. Then you can create a hook and monitor all messages sent to the application, and when you recieve the message from the second thread you can call the function in the DLL. However this solution will work only if the main application has a window.
Generally I have not tried if it is possible to hook a thread message loop. But even if it is possible, the main application must have a message loop.

So you can't find a perfect solution of what you want without cooperation with the main app
MikeP090797Author Commented:
Ok, thanks for your help.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now