Board index » cppbuilder » correct way to handle messages in the component thread

correct way to handle messages in the component thread


2005-04-28 06:21:11 PM
cppbuilder8
Is this the correct way to handle messages dispatched with PostMessage(
HWND, WM_SMS,1,1)? Is this the correct way to handle messages in a
component, it seems strange to me that you would have to override the
Applications main message thread - shouldn't each thread have its own
message queue?
__fastcall TGarySMS::TGarySMS(TComponent* Owner)
: TComponent(Owner)
{
OldEvent = Application->OnMessage;
Application->OnMessage = AppMessage;
}
void __fastcall TGarySMS::AppMessage(tagMSG &Msg, bool &Handled)
{
if (Msg.message == WM_SMS)
{
if( Msg.wParam == 1)
{
if( FOnSMSSent != NULL)
FOnSMSSent( this, (char *)Msg.lParam);
SysFreeMem( (char *)Msg.lParam );
}
Handled = true;
}
if( OldEvent != NULL)
OldEvent( Msg, Handled);
}
Thanks again everyone for all your help, I will never get this finished
without it.
 
 

Re:correct way to handle messages in the component thread

"gary" < XXXX@XXXXX.COM >wrote in message
Quote
Is this the correct way to handle messages dispatched with PostMessage(
HWND, WM_SMS,1,1)? Is this the correct way to handle messages in a
component
No. What exactly are you trying to accomplish in the first place?
Quote
it seems strange to me that you would have to override the
Applications main message thread
Components should not be assigning handlers to TApplication's events. The
events can only have 1 handler each, so if a component assigns any handlers,
the user's code won't be able to do so as well without effecting the
component.
Quote
shouldn't each thread have its own message queue?
Yes. However, the VCL only manages the main thread queue. Any worker
threads you create, you are responsible for managing yourself.
Gambit
 

Re:correct way to handle messages in the component thread

Quote
No. What exactly are you trying to accomplish in the first place?
Gambit
My non-visual component monitors a serial port. When I call an event I want
it to be outside of the components thread so that the component doesnt stop
if the user does something time consuming inside the event. Also, I want the
event code to occur in the main VCL thread so that the user doesnt have to
worry about writing thread-safe code in the event.
 

{smallsort}

Re:correct way to handle messages in the component thread

"gary" < XXXX@XXXXX.COM >wrote in message
Quote
My non-visual component monitors a serial port. When I call an
event I want it to be outside of the components thread so that the
component doesnt stop if the user does something time consuming
inside the event.
Give the component its own hidden HWND window, such as by calling the
AllocateHWnd() function, and then have the monitoring thread post messages
to that window when needed.
Gambit
 

Re:correct way to handle messages in the component thread

If your component is an descendant of TWinControl, you may overwrite the
parent's 'Dispatch' method to add custom message handlers. You may ise BCB
IDE's
class explorer if the version supports that to "Add Method" of your
component class and specify vissualy message to handle. This will generate
in your class definition BEGIN_MESSAGE_MAP / END_MESSAGE_MAP macros.
If the message is user defined it should be greater then WM_USER defined in
windows.h / vcl.h :
#define USERMSG1 WM_USER+0x01
#define USERMSG2 WM_USER+0x03
the format of Dispatch method for a control named TMyControl derived from
TBaseControl is :
void __fastcall TMyControl::Dispatch ( TMessage & msg )
{
switch ( msg.Type )
{
case WM_PAINT :
OnPaint(); /* Intercept a windows message and handle it
*/
break;
case USERMSG1 :
/* do stuff */
break;
case USERMSG2 :
/* do stuff */
break;
default :
TBaseControl::Dispatch(msg); /*Don't miss the default call
to base class for nonhandled messages
to
preserve the entire behavoir of the base control */
}
}
"gary" < XXXX@XXXXX.COM >wrote in message
Quote
Is this the correct way to handle messages dispatched with PostMessage(
HWND, WM_SMS,1,1)? Is this the correct way to handle messages in a
component, it seems strange to me that you would have to override the
Applications main message thread - shouldn't each thread have its own
message queue?

__fastcall TGarySMS::TGarySMS(TComponent* Owner)
: TComponent(Owner)
{
OldEvent = Application->OnMessage;
Application->OnMessage = AppMessage;
}

void __fastcall TGarySMS::AppMessage(tagMSG &Msg, bool &Handled)
{
if (Msg.message == WM_SMS)
{
if( Msg.wParam == 1)
{
if( FOnSMSSent != NULL)
FOnSMSSent( this, (char *)Msg.lParam);
SysFreeMem( (char *)Msg.lParam );
}
Handled = true;
}
if( OldEvent != NULL)
OldEvent( Msg, Handled);
}

Thanks again everyone for all your help, I will never get this finished
without it.