How can i get events in windows service c#

Hi,

I would like to get events from Xprotect with my windows service application.

In desktop i’ll use this code:

        private object NewEventMessageHandler(VideoOS.Platform.Messaging.Message message, FQID destination, FQID sender)
        {
            if (InvokeRequired)
            {
                Invoke(new MessageReceiver(NewEventMessageHandler), message, destination, sender);
            }
            else
            {
                var events = message.Data as System.Collections.ObjectModel.Collection<BaseEvent>;
 
....
}

But there is’nt interface on windows service, so ‘InvokeRequired’ don’t work.

How could i trigger ‘NewEventMessageHandler’?

The reason that the Alarm and Event Viewer uses Invoke is that it wants to update the UI. When you have a Windows service you do not have a UI. Whether your NewEventMessageHandler method is triggered or not must be something else not related to UI. I would start with the subscriptions and see if they are correct.

private void subscribeEvents()
{
    if (_obj3 == null)
    {
        _obj3 = _messageCommunication.RegisterCommunicationFilter(NewEventMessageHandler,
            new VideoOS.Platform.Messaging.CommunicationIdFilter(VideoOS.Platform.Messaging.MessageId.Server.NewEventsIndication), null, EndPointType.Server);
    }
}

I recommend that you explore the Alarm and Event Viewer sample. If it works for events unmodified, then the same code should work in your service (but excluding the code that updates UI).

Thank you so much,

I actually call the subscribeEvents() procedure.

My code is from this example, and works fine if, for example, I cause the event like this:

MessageReceiver mr = new MessageReceiver(NewEventMessageHandler);

Without in NewEventMessageHandler() :

            if (InvokeRequired)
            {
                Invoke(new MessageReceiver(NewEventMessageHandler), message, 
                destination, sender);
            }

I could possibly put this code in a loop to listen to new events, but I do not find it clean.

            while (true)
            {
                    MessageReceiver mr = new MessageReceiver(NewEventMessageHandler);
                    Thread.Sleep(500);
                    mr = null;
            }

It works, but i don’t like this.

I’m looking for a cleaner way to trigger NewEventMessageHandler ().

The events come from the XProtect VMS. There should be new events all the time, but if the VMS is small you can always generate events by triggering user-defined events or analytics events.

Don’t you see events happening? (Is this different in the unmodified sample compared to your service?)

With the method I presented above, I correctly receive all user_defined_events from XProtect. It’s this loop system for “NewEventMessageHandler” calling that does not suit me.

You should not have that loop. To me that loop does not make sense. Please go one step back, does the Alarm and Event Viewer sample work for you (unmodified)?

Yes it works, I’m sorry, I did not understand the call of ‘NewEventMessageHandler’ in ‘subscribeEvents’

        private void subscribeEvents()
        {
            if (_communicationObject == null)
            {
                _communicationObject = MilesComAlarmEvents.MessageCommunication.RegisterCommunicationFilter(NewEventMessageHandler,
                    new CommunicationIdFilter(MessageId.Server.NewEventsIndication), null, EndPointType.Server);
            }
        }

It works very well like this.

Thanks for everything, I discover the Framework SDK …