Difference between RegisterCommunicationFilter() and RegisterReceiver()

// 1
MessageCommunicationManager.Start(EnvironmentManager.Instance.MasterSite.ServerId);
_messageManager = MessageCommunicationManager.Get(EnvironmentManager.Instance.MasterSite.ServerId);
_eventsFilter = _messageManager.RegisterCommunicationFilter(NewEventIndicationMessageHandler, new CommunicationIdFilter(MessageId.Server.NewEventIndication));
 
// 2
_eventsFilter = EnvironmentManager.Instance.RegisterReceiver(NewEventIndicationMessageHandler, new MessageIdFilter(MessageId.Server.NewEventIndication));

Hello,

In an attempt to solve “The calling thread must be STA, because many UI components require this.” in a smart client background plugin I fell on https://developer.milestonesys.com/s/question/0D50O00004N1u5LSAR/getting-a-threading-error-on-a-sc

This confuses me because I solved my problem by using the method 1 and wrapping my code inside ClientControl.Instance.CallOnUiThread(), but it looks like in the other thread he solved it by using RegisterReceiver().

Which one should I use? They look identical in what they achieve so I’m confused.

The short answer is that you can use both, but MessageCommunication is the conceptually right choice.

The slightly longer explanation is that MessageCommunication is to be used when you want to communicate with something outside the local application, whereas EnvironmentManager communication methods are for local communication within the current application.

However, for historic reasons there exists local handling of certain messages, that are obviously none-local. Examples are event and alarm listening, event triggering and PTZ commands. The local handler will pick these up and make a call to the appropriate service. Therefore you can also use the EnvironmentManager approach for these.

A small extra note: It is recommended to register for NewEventsIndication instead of NewEventIndication as that will cause the server to package multiple events together in one message if they are all ready at the same time, whereas NewEventIndication will send them one by one. So there is a small performance gain in using NewEventsIndication. If you do this change be aware that the response type is slightly different as it will return a list of events instead of just one.

Thanks for the clarifications.