System.Messaging.TMessageManager.SubscribeToMessage

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function SubscribeToMessage(const AMessageClass: TClass; const AListener: TMessageListener): TMessageSubscriptionId; overload; virtual;
function SubscribeToMessage(const AMessageClass: TClass; const AListenerMethod: TMessageListenerMethod): TMessageSubscriptionId; overload; virtual;

C++

virtual TMessageSubscriptionId __fastcall SubscribeToMessage(const System::TClass AMessageClass, const _di_TMessageListener AListener)/* overload */;
virtual TMessageSubscriptionId __fastcall SubscribeToMessage(const System::TClass AMessageClass, const TMessageListenerMethod AListenerMethod)/* overload */;

Properties

Type Visibility Source Unit Parent
function public
System.Messaging.pas
System.Messaging.hpp
System.Messaging TMessageManager

Description

Subscribes a listener to a message.

Call SubscribeToMessage to subscribe message-handling methods to specific types of messages on a message manager.

To unsubscribe a previously-subscribed method from a message type, call Unsubscribe.

Example

The example below shows how to subscribe an anonymous method. The example subscribes the anonymous method to messages containing an Unicode string. When a message of this type is sent to the message manager, the anonymous method shows a dialog box displaying the string from the message.

In Delphi:

// var SubscriptionId: Integer;
SubscriptionId := MessageManager.SubscribeToMessage(TMessage<UnicodeString>, procedure(const Sender: TObject; const M: TMessage)
begin
  ShowMessage((M as TMessage<UnicodeString>).Value);
end);

In C++:

1. Define a method in your class with the following signature:
void __fastcall TForm1::ShowReceivedMessage(System::TObject* const Sender, System::Messaging::TMessage* const M)
{
  TMessage__1<UnicodeString>* Message = dynamic_cast<TMessage__1<UnicodeString>*>(M);
  if (Message)
    ShowMessage(Message->Value);
}
2. Then subscribe this method as follows:
TMessageManager* MessageManager = TMessageManager::DefaultManager;
TMetaClass* MessageClass = __classid(TMessage__1<UnicodeString>);
TMessageListenerMethod ShowReceivedMessagePointer = &(this->ShowReceivedMessage);
int SubscriptionId = MessageManager->SubscribeToMessage(MessageClass, ShowReceivedMessagePointer);

See Also