JanvanderVen

Problem with IEventPipe2.AddSignUp

Discussion created by JanvanderVen on Aug 5, 2010
Latest reply on Aug 6, 2010 by spilon

Dear List,

 

 

 

I am writing an SDK based application that monitors tags through a PointList's EventPipe.

 

The number of tags increases during the use of the service. I provide a web service that allows other applications to subscribe to tags they are interested in. The problem is that when a new tag is monitored, the event is fired the same number of times as there are items in the PointList.

 

I have a couple of fields in my C# class:

private PISDK.PointList pointList = new PISDK.PointList();
PISDK._DEventPipeEvents_Event _ptEvent;
PISDK.IEventPipe2 _pipe;

And the event:

public event TriggerActivatedHandler TriggerActivatedEvent;

The delegate is defined in an interface that my class implements:

public delegate void TriggerActivatedHandler(string thing, object value, DateTime timeStamp);

And these are used in response the subscription request, like this:

public void Subscribe(string tag)
{
    if (pointList.Count > 0)
    {
        // reuse the defined _pipe
        _pipe.AddSignUp(sdk.Servers[piserver].PIPoints[tag], null);
    }
    else
    {
        try
        {
            pointList.Add(sdk.Servers[piserver].PIPoints[tag]);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        _pipe = (PISDK.IEventPipe2)pointList.Data.EventPipe;
        _ptEvent = (PISDK._DEventPipeEvents_Event)_pipe;
        _ptEvent.OnNewValue += new PISDK._DEventPipeEvents_OnNewValueEventHandler(_ptEvent_OnNewValue);
    }
}

Now, whenever one of the monitored tags fires, the _ptEvent assumes that there are as many listeners as there are points in the PointList.

 

This is the event handler:

void _ptEvent_OnNewValue()
{
    if (this.TriggerActivatedEvent == null)
    {
        // if nobody listens we have to do nothing
        return;
    }
    if (_pipe.Count == 0)
    {
        return;
    }
    Array events = _pipe.TakeAll();
    foreach (PISDK.PIEventObject peo in events)
    {
        PISDK.PointValue pv = (PISDK.PointValue)peo.EventData;
        this.TriggerActivatedEvent(pv.PIPoint.Name, pv.PIValue.Value.ToString(), pv.PIValue.TimeStamp.LocalDate); // this is the function that fires too often?
    }
}

So, I get too many events.

 

The behaviour is identical, if you first do a _ptEvent.OnNewValue -= to remove the previous handler, and then do a += is again on the updated PointList.

 

I do not quite understand what is going wrong.

 

Please advise.

 

Kind regards,
Jan van der Ven

 

 

Outcomes