?
Solved

PostMessage memory handling

Posted on 2003-12-02
12
Medium Priority
?
389 Views
Last Modified: 2013-11-20
I have a MDI application with a worker thread receiving approximately 3000 messages per minute from a socket. Some of the messages have to be processed by Child Windows if they are active. A message could apply to more than one child window.

If the worker thread post a message to the parent window, when and where can I delete the message created in the worker thread, as the worker thread doesn’t know when the child windows have finished their processing?

PS: I don't need to use SendMessage.
0
Comment
Question by:DBOTMA
[X]
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
  • 6
  • 5
12 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864076
Use PostMessage itslef, but add your string messages to a global array and pass that index thru PostMessage. When the child accepts the message, process, delete from there itself.
0
 

Author Comment

by:DBOTMA
ID: 9864189
Your message: "delete from there itself", where is the "there"?  Do you mean from the child window, if so, what if other child windows still reading/processing the message? ALSO if the receiving window/process has to delete the messages what will happen with messages still in the  queue if the window close.  
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864248
If you know the count of child windows, you can use a reference count that will decrement on each child process, delete when that count becomes zero. I donno know how much this suitable for your project, just a guess
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:DBOTMA
ID: 9864463
1. I can't wait for all the child windows to close before I delete the messages, the array will be too large. Thus, I have to delete messages once they are served by all the open child windows.

2. This is still unanswered: "ALSO if the receiving window/process has to delete the messages what will happen with messages still in the  queue if the window close." ie it' won't be deleted
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864529
Please give some more idea on ur application

1. is that *message* you specified is a heap allocated memory?
2. Which parent window accepting this messages..? CChildFrame?..if yes, please mention all typ child controls that have this parent.

Rosh :)
0
 

Author Comment

by:DBOTMA
ID: 9864755
This is a snippet, hope it will make it more clear :

Global Variable
===============

CWnd * gpWnd;

Worker Thread
=============
VOID WorkerThread::SendMessage()
{
                  CString* tmpMsg = new CString();   //I think, This will become an array

                  tmpMsg->Append("....Message read from socket.....");

                  EnumChildWindows(gpWnd->m_hWnd, EnumUpdateChildProc, (LPARAM) &tmpMsg);
}


BOOL CALLBACK EnumUpdateChildProc(HWND hwnd,    LPARAM lParam )
{
      if (!(gpWnd == NULL))
            PostMessage(hwnd, WM_SECURITY_UPDATE, 0, lParam);
      
      return TRUE;
}


MDI
===

BEGIN_MESSAGE_MAP(CMyRecView, CRecordView)
      ON_MESSAGE(WM_SECURITY_UPDATE, OnUpdate)
END_MESSAGE_MAP()


void CMyRecView::OnInitialUpdate()
{
      m_pSet = &GetDocument()->m_WatchlistRecSet;
      CRecordView::OnInitialUpdate();

      gpGridWatchlist = FALSE;

      if (gpWnd== NULL)
            gpWnd= GetParentOwner();
}


LONG CMyRecView::OnUpdate(WPARAM, LPARAM lParam)
{
      
      .....Update grid on child window .....
      
      return 0l;
}
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864884
Another try out

simply define some strings like

#define IDS_MESSAGE_INITIALIZE       "....Initializet....."
#define IDS_MESSAGE_CONNECTED      "....Connected....."
#define IDS_MESSAGE_INITIALIZE      "....Message read from socket....."
#define IDS_MESSAGE_COMPLETED      "....Completed....."


etc

and pass a number in the PostMessage, from that number you can identify which string you needed from that define....So the strings will not be duplicated and no Strings will be allocated in heap for deletion

Good luck
0
 

Author Comment

by:DBOTMA
ID: 9865049
I'm lost...........(a few more point might make it worth the effort)


Please note the code :

 tmpMsg->Append("....Message read from socket.....");

is more like:

            for(;;){
                  CString* tmpMsg = new CString();  
                  ...
                  ...
                  recv(hSocket, &msgRead, byteRx,0)
                                                ...
                                                ...
                  tmpMsg->Append(msgRead);

                  //PostMessage in EnumUpdateChildProc
                  EnumChildWindows(gpWnd->m_hWnd, EnumUpdateChildProc, (LPARAM) &tmpMsg);
            }

the string "....Message read from socket....." is not what I want to send but the message read from the socket.

Thanks

0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9865097
What about this

VOID WorkerThread::SendMessage()
{
      CString* tmpMsg = new CString();   //I think, This will become an array

      tmpMsg->Append("....Message read from socket.....");

      EnumChildWindows(gpWnd->m_hWnd, EnumUpdateChildProc, (LPARAM) &tmpMsg);
}


BOOL CALLBACK EnumUpdateChildProc(HWND hwnd,    LPARAM lParam )
{
    if (!(gpWnd == NULL))
      {
            CString *pNewString = new CString();
            CString* tmpMsg = (CString* )lParam;

            pNewString->Append(tmpMsg);

            delete tmpMsg;

         PostMessage(hwnd, WM_SECURITY_UPDATE, 0, (LPARAM)pNewString);
      }
   
     return TRUE;
}


LONG CMyRecView::OnUpdate(WPARAM, LPARAM lParam)
{

      CString* tmpMsg = (CString* )lParam;
   
     .....Update grid on child window .....

       delete tmpMsg;
   
     return 0l;
}
0
 

Author Comment

by:DBOTMA
ID: 9865207
More Points... :-)

Ok, now we are were we started.

How I understand MDI is that  the function CMyRecView::OnUpdate(WPARAM, LPARAM lParam)  will apply to each instanciation of a Child Window. For every new child window this function OnUpdate will execute if the worker thread sends a message. ie more than one Child Window will receive the message and each one will delete the tmpMsg which I think will cause failure.

   
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 1000 total points
ID: 9865234
Yes thatz Y I created a new CString in the EnumUpdateChildProc ...please look the EnumUpdateChildProc function. (CString *pNewString = new CString()) ... Creating a new CString for each child, so it will not fail.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9865301
What about posting the message to a central document like class. This class then keeps track of all messages received. All open views need to update their view using the data stored in this class. That way the worker thread is sending the message to only one recipient which knows it can delete the CString object. And all views can read the data. Since now the document class and the views are in the same thread a synchronisation should not be necessary.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

800 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