Message delay and merge using Windows message loop

Source: Internet
Author: User
The initial solution is to set a time interval for computing messages: When a computing message arrives, the time starts. during a certain period of time, the received computing messages are not processed. Because the time interval cannot be specified, you cannot make a budget. The time interval setting is too long. If the two operations are too fast, the calculation will be missed. The setting time is too short to solve the problem of excessive calculation. Therefore, this solution can only be regarded as a delay, but not a solution.

Later, I thought of the following methods to solve the problem perfectly. This is a souvenir for me, cainiao.

My solution is to create a class for merging messages. With the Window message loop, when the message arrives, it is not processed immediately, but the parameter is saved. If it is the first message, Post a processing message to yourself. When the Post processing message arrives, take the function that the real call should call.

Later, I added modules that required this feature, And I implemented it as a Tool class that can accommodate messages to be merged. The Code is as follows:
 

//************************************** **************************************
// D l g C s o f t w a r e c o.
//************************************** **************************************
// Filename: fepmergemessage. h
// Project: FEP V4 6.0
// Module:
// Programmer: Ma Nan
// Version: Version 1.00
// Revision date: 2006/12/04
//************************************** **************************************
// Description: Declaration of class cfepmergemessage
//************************************** **************************************
// Revision history:
// 2006/12/04-first implementation
//************************************** **************************************
// Bugs :........
//************************************** **************************************
// @ Doc
//************************************** **************************************
// FepMergeMessage. h: interface for the CFepMergeMessage class.
//
//////////////////////////////////////// //////////////////////////////

# If! Defined (partition _)
# Define _

# If _ MSC_VER> 1000
# Pragma once
# Endif // _ MSC_VER> 1000

Class MERGE_MES_HEADER;
Typedef void (* MergeMesCallBack) (const MERGE_MES_HEADER * lpMergMes );

Class MERGE_MES_HEADER: public CObject
{
DECLARE_DYNAMIC (MERGE_MES_HEADER );

Mergemescallback pfncallback;

Virtual merge_mes_header * clone () const = 0;
Virtual bool issamemes (const merge_mes_header * PMEs) const = 0;
};

//************************************** **************************************
// @ Class cfepmergemessage |
// Cfepmergemessage is used
//
// @ Base cdialog
//************************************** **************************************
// @ Ex
// This shows how to call cfepmergemessage: |
//
//
//
//************************************** **************************************
// @ Prog
// Ma Nan
// @ Revs
// 2006/12/04-first implementation
//************************************** **************************************
// @ Todo
//************************************** **************************************
Class CFepMergeMessage: public CDialog
{
Public:
CFepMergeMessage ();
Virtual ~ CFepMergeMessage ();

Static BOOL Excute (const MERGE_MES_HEADER * pParam );

Protected:
// Create the dialog to deal the message
BOOL Create ();

Protected:
Afx_msg LRESULT OnMergeMessage (WPARAM wParam, LPARAM lParam );
Afx_msg LRESULT OnProcessMessage (WPARAM wParam, LPARAM lParam );
DECLARE_MESSAGE_MAP ()
Protected:
CArray <MERGE_MES_HEADER *, MERGE_MES_HEADER *> m_rgMessage;
BOOL m_bMessagePosted;

};

# Endif //! Defined (partition _)

/////////////////////////////
// Fepmergemessage. cpp
////////////////////////////
# Include "stdafx. H"
# Include "fepmergemessage. H"
# Include "../include/fepptr. H"

# Ifdef _ debug
# UNDEF this_file
Static char this_file [] =__ file __;
# Define new debug_new
# Endif

# Define wm_merge_message wm_user+ 100
# Define wm_process_message wm_user+ 200
//////////////////////////////////////// //////////////////////////////////
// Struct merge_mes_header
//////////////////////////////////////// //////////////////////////////////
Implement_dynamic (merge_mes_header, cobject)

//////////////////////////////////////// //////////////////////////////
// Construction/Destruction
//////////////////////////////////////// //////////////////////////////
Static CFepPtr <CFepMergeMessage> pDlgMergeMessage (NULL );

CFepMergeMessage: CFepMergeMessage ()
{
M_bMessagePosted = FALSE;
Create ();
}

CFepMergeMessage ::~ CFepMergeMessage ()
{
For (int I = 0; I <m_rgMessage.GetSize (); ++ I)
{
Delete m_rgMessage.GetAt (I );
}
M_rgMessage.RemoveAll ();
}

BEGIN_MESSAGE_MAP (CFepMergeMessage, CDialog)
ON_MESSAGE (WM_MERGE_MESSAGE, OnMergeMessage)
ON_MESSAGE (WM_PROCESS_MESSAGE, OnProcessMessage)
END_MESSAGE_MAP ()

//************************************** **************************************
// Function CFepMergeMessage: Create
// @ Mfunc
// @ Rdesc TRUE if the function succeeds; otherwise, FALSE. (BOOL)
// @ Xref <c CFepMergeMessage>
//************************************** **************************************
// @ Prog
// Ma Nan
// @ Revs
// 2006/12/04 First implementation
//************************************** **************************************
// @ Todo
//
//************************************** **************************************
BOOL CFepMergeMessage: Create ()
{
If (GetSafeHwnd ()! = NULL)
{
Return TRUE;
}
DWORD dwDlgTempl [100];
ZeroMemory (dwDlgTempl, sizeof (DWORD) * 100 );
DLGTEMPLATE * pDlgTmp = (DLGTEMPLATE *) dwDlgTempl;
PDlgTmp-> style = WS_POPUP | DS_CONTROL;
PDlgTmp-> cx = 1;
PDlgTmp-> cy = 1;
Return CDialog: CreateIndirect (pDlgTmp );
}

//************************************** **************************************
// Function CFepMergeMessage: Excute
// @ Mfunc
// @ Rdesc TRUE if the function succeeds; otherwise, FALSE. (BOOL)
// @ Parm const MERGE_MES_HEADER * | pParam | a pointer to const MERGE_MES_HEADER
// @ Xref <c CFepMergeMessage>
//************************************** **************************************
// @ Prog
// Ma Nan
// @ Revs
// 2006/12/04 First implementation
//************************************** **************************************
// @ Todo
//
//************************************** **************************************
BOOL CFepMergeMessage: Excute (const MERGE_MES_HEADER * pParam)
{
// DlgMergeMes. SendMessage (WM_MERGE_MESSAGE, (WPARAM) pParam, 0 );
If (pDlgMergeMessage. m_pPtr = NULL)
{
PDlgMergeMessage. m_pPtr = new CFepMergeMessage;
}
If (pDlgMergeMessage-> GetSafeHwnd () = NULL)
{
Return FALSE;
}
PDlgMergeMessage-> SendMessage (WM_MERGE_MESSAGE, (WPARAM) pParam, 0 );
Return TRUE;
}

//************************************** **************************************
// Function CFepMergeMessage: OnMergeMessage
// @ Mfunc
// @ Rdesc (LRESULT)
// @ Parm WPARAM | wParam |
// @ Parm | LPARAM |
// @ Xref <c CFepMergeMessage>
//************************************** **************************************
// @ Prog
// Ma Nan
// @ Revs
// 2006/12/04 First implementation
//************************************** **************************************
// @ Todo
//
//************************************** **************************************
LRESULT CFepMergeMessage: OnMergeMessage (WPARAM wParam, LPARAM)
{
Const MERGE_MES_HEADER * pNewMes = (const MERGE_MES_HEADER *) wParam;
For (int I = 0; I <m_rgMessage.GetSize (); ++ I)
{
MERGE_MES_HEADER * pMes = m_rgMessage.GetAt (I );
If (pNewMes-> IsKindOf (pMes-> GetRuntimeClass () & pNewMes-> IsSameMes (pMes ))
{
Delete pMes;
M_rgMessage.RemoveAt (I );
-- I;
}
}
M_rgMessage.Add (pNewMes-> Clone ());
If (! M_bMessagePosted)
{
PostMessage (WM_PROCESS_MESSAGE, 0, 0 );
M_bMessagePosted = TRUE;
}
Return 0L;
}

//************************************** **************************************
// Function CFepMergeMessage: OnProcessMessage
// @ Mfunc
// @ Rdesc (LRESULT)
// @ Parm | WPARAM |
// @ Parm | LPARAM |
// @ Xref <c CFepMergeMessage>
//************************************** **************************************
// @ Prog
// Ma Nan
// @ Revs
// 2006/12/04 First implementation
//************************************** **************************************
// @ Todo
//
//************************************** **************************************
LRESULT CFepMergeMessage: OnProcessMessage (WPARAM, LPARAM)
{
While (m_rgMessage.GetSize ()> 0)
{
MERGE_MES_HEADER * pHeader = m_rgMessage.GetAt (0 );
PHeader-> pfnCallBack (pHeader );
Delete pHeader;
M_rgmessage.removeat (0 );
}
M_bmessageposted = false;
Return 0l;
}

Note:

MERGE_MES_HEADER is a parameter class. To use this tool class, you must derive a parameter class.

The following tasks must be completed for the parameter class:

1. Execute the function.

2. Whether it is the ID of the same message.

This parameter class consists of the following parts:

1. pfnCallBack. Callback function: the function to be executed.

2. Clone (). This is a virtual function, because this parameter needs to be stored. You can use this function to copy parameters.

3. IsSameMes (). This is a virtual function. When CFepMergeMessage calls this function, it has determined the type. Rewrite this function to determine whether the input parameter is the same.

These three variables/virtual functions must be copied or overwritten in the derived class.

It is easy to use. A parameter object is generated and CFepMergeMessage: Excute (const MERGE_MES_HEADER *) is called *).
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.