0

Is there a way to create a callback function that fires when a list object (any Class implementing the ICollection interface of C#.NET) reaches a certain capacity (number of items in the list)?

I would like an event to fire when my list object has 5 elements for example.

To place you in context, I am doing that to batch data processing since I am running a service that utilizes HTTP over the network and batching is necessary for the performance of my application.

a simple implementation is batching in memory (in a list) and fire when it reaches some capacity and then send over the network or if there are other tools I would appreciate any pointers to them.

Thanks

Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
  • I don't know of a built-in implementation (`IObservableCollection` will notify you of all changes) but creating one should be quite easy. Just create a class with a private `ICollection` member, and an `Add()` method that keeps track of the items added. Throw in an "AddThresholdReached` event and you're all set. – dlev Jun 29 '12 at 21:15
  • `IObservableCollection ` sounds like what I need to simply implement a batch operation – Saher Ahwal Jun 29 '12 at 21:25
  • ICollection is an interface meaning it has no functionality. The only thing you know about an instance is that it implements the ICollection interface and nothing else. ICollection has no events and therefor you cannot get any events from it. However, you could create your own collection by inheriting e.g. List and implement your own ICollection.Add method. – Casperah Jun 29 '12 at 21:25
  • @Casperah : I know that, that is why I say : any class that implements the ICollection interface ... – Saher Ahwal Jun 29 '12 at 21:26
  • @Saher It's actually a concrete class, `ObservableCollection`. I mistyped earlier (and you can't edit comments if they're more than 5 minutes old.) It's definitely useful, and you could probably build an implementation on top of it. – dlev Jun 29 '12 at 21:46
  • I noticed that. Thanks for clarifying that as well. – Saher Ahwal Jun 29 '12 at 22:23

2 Answers2

2

There's no way to add the event to every ICollection class that already exists, but you could use either ObservableCollection or BindingList as alternatives. Both of those will raise an event when the list changes so you could just check the item count in the changed event. Alternatively, you could create your own class that implements ICollection simply wrapping the methods of an internal List object and add special code to the Add method to check the count.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
1

You can implement the event:

public class YourClassName
{
    private List<Type> m_InnerList = new List<Type>();

    public event EventHandler OnBatch;

    public void Add(Type object)
    {
        m_InnerList.Add(object);
        if ((m_InnerList.Count % YourCountHere == 0) && OnBatch != null)
           OnBatch(this, new EventArgs());

    }

}

You will have to change the name, choose your type (or make the class generic too), implement more logic (like accessing your items) - and you can change it to get the batch amount in the constructor

you can derive from List instead

eyossi
  • 4,230
  • 22
  • 20
  • This should probably check for *multiples* of the batch size; this example will only fire the event once. – dlev Jun 29 '12 at 21:17
  • @dlev I didn't mean to implement his logic - just to give an example of a way to do that... but i changed it anyway – eyossi Jun 29 '12 at 21:21