Solved

PostThreadMessage question

Posted on 2000-03-20
11
787 Views
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
{
public:
      void SyncExecute(void*, LONG);
      void AsyncExecute(void*, void*);
      BOOL InitInstance();
      BOOL ExitInstance();
      
protected:
      //{{AFX_MSG(CDll)
            // 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);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()


typedef void(NPROC)(LONG);

BEGIN_MESSAGE_MAP(CDll, CWinApp)
      //{{AFX_MSG_MAP(CDll)
            // 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)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()




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

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




---
Thanks in advance

0
Comment
Question by:MikeP090797
11 Comments
 
LVL 7

Expert Comment

by:tchalkov
Comment Utility
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
0
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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(....);


Regards
W.Yinan
(I'm not a qualified MFC expert,if wrong,please correct me:)
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
No need to correct Wyn's comment ... CWinThread::PostThreadMessage() sends the message to the thread represantated by the CWinThread object used with the call.
0
 
LVL 7

Expert Comment

by:tchalkov
Comment Utility
The fact that the application has an object derived from CWinApp does not mean that it has a message loop
0
 
LVL 8

Author Comment

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

Thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:tchalkov
Comment Utility
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.
0
 
LVL 8

Author Comment

by:MikeP090797
Comment Utility
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..


Thanks
0
 
LVL 7

Accepted Solution

by:
tchalkov earned 200 total points
Comment Utility
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.
0
 
LVL 8

Author Comment

by:MikeP090797
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:tchalkov
Comment Utility
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
0
 
LVL 8

Author Comment

by:MikeP090797
Comment Utility
Ok, thanks for your help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now