Serial42/Serial.cs class duplicate registrations to Interface.LineReceived


The Serial42/Serial.cs class allows duplicate registrations of itself to Interface.LineReceived. This is the latest gadgeteer source (gadgeteer-32422, https://gadgeteer.codeplex.com/SourceControl/latest#Main/GadgeteerCore/Libraries/Core/Serial42/Serial.cs)
I believe the registration of the Interface.LineReceived event was meant to be handled in the AutoReadLineEnabled method (using the value of _autoReadLineEnabled to track if it is currently registered).
However, inside: public event LineReceivedEventHandler LineReceived, it also registers the Serial class with Interface.LineReceived when it adds the first Event handler.

The following user Serial Class code results in doubled events: (it would probably be OK if you swapped these two statements and didn't ever change the LineReceived event)
serialPort.AutoReadLineEnabled = true;
serialPort.LineReceived += serialPort_LineReceived;
the LineReceived Eventr's add/remove blocks in Serial.cs should let AutoReadLineEnable handle the Interface.LineRecieved registration. The following modified code for 'add' behaves correctly for my tests. (similar change for 'remove' is also needed)
    if (value == null)
    if (_lineReceivedHandler == null)
        AutoReadLineEnabled = true;  // this method handles Interface.LineRecieved registration
    _lineReceivedHandler += value;


jamesscott wrote Feb 28, 2014 at 5:43 PM

Thank you for this report. We fixed this as you suggested in 2.43.900.