PostThreadMessage question

Posted on 2000-03-20
Last Modified: 2013-11-20
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

Question by:MikeP090797
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 2636259
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

Expert Comment

ID: 2636465
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:)
LVL 31

Expert Comment

ID: 2636538
No need to correct Wyn's comment ... CWinThread::PostThreadMessage() sends the message to the thread represantated by the CWinThread object used with the call.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2636943
The fact that the application has an object derived from CWinApp does not mean that it has a message loop

Author Comment

ID: 2637161
The application is a DLL, forgot to mention that. GetLastError returns 0. How do I create a message queue for the thread?


Expert Comment

ID: 2639165
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.

Author Comment

ID: 2639716
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..


Accepted Solution

tchalkov earned 200 total points
ID: 2639731
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.

Author Comment

ID: 2639948
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.

Expert Comment

ID: 2640266
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

Author Comment

ID: 2640355
Ok, thanks for your help.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question