Implementing message latency and merging with Windows message loops

Source: Internet
Author: User
Tags bool time interval

The initial solution was to set a time interval for computing messages: When a computed message arrives, it starts to tick, and at a certain time interval, the computed message received is not processed. Because the time interval cannot be given, it is impossible to budget. The time interval setting is long, if two times operation too fast, will leak calculates, the setting time is short, and cannot solve the problem that calculates this number too many. So this solution can only be a delay in the problem, but not a solution.

Later, do not know when Brainwave, think of the following method, the perfect solution to the problem. This is for me this rookie, still has the commemorative meaning.

My solution is to create a class that merges messages. With the window message loop, when the message arrives, it is not processed immediately, just the parameters are saved. If this is the first message, post a processing message to yourself. The function that should be called when the processing message of post arrives.

Later, the requirement to use this feature increased, and I realized it as a tool class that can accommodate multiple messages that need 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 (Afx_fepmergemessage_h__d60309aa_9da0_4474_9dc0_edff72e7165c__included_)
#define Afx_fepmergemessage_h__d60309aa_9da0_4474_9dc0_edff72e7165c__included_
#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 to
//
@base CDialog
//****************************************************************************
@ex
This shows you 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 (AFX_FEPMERGEMESSAGE_H__D60309AA_9DA0_4474_9DC0_EDFF72E7165C__INCLUDED_)
/////////////////////////////
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-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-&GT;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-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-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-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;
}

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.