From Appmethod Libraries
Jump to: navigation, search

Object Pascal

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


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


Type Visibility Source Unit Parent
function public
System.Messaging TMessageManager


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.


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 Object Pascal:

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

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)
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