I am writing a data acquisition application. I wondering whether I should use one event multiple subscribers or multiple events multiple subscribers. I am worried about performance. Also could I improve performance by offloading the subscribers to a different thread?
Here is an example of what I mean by multiple events Multiple subscribers.
public delegate void EventChannelUpdated(object sender, EventArgsChannelUpdated e);
public class EventArgsChannelUpdated : EventArgs
{
public chCell chCell;
}
public delegate void EventLimitExceeded(object sender, EventArgsLimitExceeded args);
public class EventArgsLimitExceeded : EventArgs
{
}
class Example
{
public List<Channel> AllChannels;
}
public class Saver_allChannels
{
private Example example;
public event EventChannelUpdated eventChannelUpdated;
public void DataAdded()
{
//** this part is pseudo code as I have simplified some of the hwObjs structure
for (int i = 0; i < hwObjs.Count; i++)
{
example.ListOfchannels[i].addSamples(hwObjs[i].Reading);
}
}
}
public class Channel
{
public List<chCell> column { get; set; }
public string label { get; set; }
public event EventChannelUpdated eventChannelUpdated;
public double multipler { get; set; }
public void addSamples(double unScaledValue)
{
chCell temp = new chCell() {channel = this, scaledValue = unScaledValue*multipler};
column.Add(temp);
eventChannelUpdated(this,new EventArgsChannelUpdated(){chCell = temp});
}
}
public class chCell
{
public double scaledValue { get; set; }
public Channel channel { get; set; } //** parent Channel List
}
public class Monitor
{
public double Limit;
public event EventLimitExceeded eventLimitExceed;
public void Handle_EventChannelUpdated(object sender, EventArgsChannelUpdated args)
{
if (args.chCell.scaledValue > Limit)
{
eventLimitExceed(this, new EventArgsLimitExceeded());
}
}
}
Here is an example of what I mean by one event multiple subscribers
public delegate void EventChannelUpdated(object sender, EventArgsChannelsUpdated e);
public class EventArgsChannelsUpdated : EventArgs
{
public List<chCell> RowOfchCells;
}
public delegate void EventLimitExceeded(object sender, EventArgsLimitExceeded args);
public class EventArgsLimitExceeded : EventArgs
{
}
class Example
{
public List<Channel> ListOfchannels;
}
public class Saver_allChannels
{
Example example;
public event EventChannelUpdated eventChannelUpdated;
public void DataAdded()
{
List<chCell> LastRow = new List<chCell>();
//** this part is pseudo code as I have simplified some of the hwObjs structure
for (int i = 0; i < hwObjs.Count; i++)
{
example.ListOfchannels[i].addSamples(hwObjs[i].Reading);
LastRow.Add(new chCell() {channel = example.ListOfchannels[i], Value = hwObjs[i].Reading});
}
eventChannelUpdated(this, new EventArgsChannelsUpdated() { RowOfchCells = LastRow });
}
}
public class Channel
{
public List<chCell> column { get; set; }
public string label { get; set; }
public double multipler { get; set; }
public void addSamples(double unScaledValue)
{
chCell temp = new chCell() {channel = this, scaledValue = unScaledValue*multipler};
column.Add(temp);
}
}
public class chCell
{
public double scaledValue { get; set; }
public Channel channel { get; set; } //** parent Channel List
}
public class Monitor
{
public double Limit;
public Channel subbedChannel;
public event EventLimitExceeded eventLimitExceed;
public void Handle_EventChannelUpdated(object sender, EventArgsChannelsUpdated args)
{
double value = (args.RowOfchCells.FirstOrDefault(x => x.channel == subbedChannel)).scaledValue;
//** do something with value
}
}