c# Fire an event when Collection capacity is reached or timeout occurs -


i need execute when collection capacity reached or timeout occurs.

for example, if had list capacity of 10 , timeout of 10 seconds have wait 1 of "events" raised execute something.

i thinking on use observablecollection , in event collectionchanged() count number of items of collection , see if equal number max of requests allowed.

at same time have timer control timeout.

can please tell me if there better solution?

thanks lot in advance.

you can implement own collection features need. this:

  static void main()         {             var list = new myobservablecollection<int>(10, 10);              list.conditionreachedevent += () =>             {                 //do             };              list.starttimer();              task.factory.startnew(                 () =>                     {                         // simulate slow operation                         thread.sleep(12000);                         (var = 0; < 10; i++)                         {                             list.add(i);                         }                     });              console.read();         }          public sealed class myobservablecollection<t> : system.collections.objectmodel.observablecollection<t>         {             public event action conditionreachedevent = delegate { };             private system.threading.timer timer;             private readonly int alertthreshold;             private long iseventraised = 0;             private readonly int timeout = 0;              public myobservablecollection(int alertthreshold, int timeout)             {                 this.alertthreshold = alertthreshold;                 this.timeout = timeout * 1000;             }              public void starttimer()             {                 interlocked.exchange(ref this.iseventraised, 0);                 this.stoptimer();                 this.timer = new timer(x =>                 {                     this.raiseevent();                 }, null, this.timeout, this.timeout);             }              private void stoptimer()             {                 if (this.timer != null)                 {                     this.timer.dispose();                     this.timer = null;                 }             }              protected override void oncollectionchanged(system.collections.specialized.notifycollectionchangedeventargs e)             {                 base.oncollectionchanged(e);                 if (this.count >= this.alertthreshold)                 {                     this.raiseevent();                 }             }              private void raiseevent()             {                 this.stoptimer();                  if (interlocked.compareexchange(ref this.iseventraised, 1, 0) != 0)                 {                     return;                 }                  this.conditionreachedevent();             }         } 

with async/await

 static void main()         {             var list = new myobservablecollection<int>(10);             list.conditionreachedevent += () =>              {               };              list.start(10);              task.run(                 () =>                 {                     // simulate slow operation                     thread.sleep(timespan.fromseconds(12));                     (var = 0; < 10; i++)                     {                         list.add(i);                     }                 });              console.read();         }           public sealed class myobservablecollection<t> : system.collections.objectmodel.observablecollection<t>         {             public event action conditionreachedevent = delegate { };             private readonly int alertthreshold;             private readonly taskcompletionsource<object> capacityreached = new taskcompletionsource<object>();              public myobservablecollection(int alertthreshold)             {                 this.alertthreshold = alertthreshold;             }              public async void start(int timeout)             {                 var timeouttask = task.delay(timespan.fromseconds(timeout));                 var capacityreachedtask = capacityreached.task;                  await task.whenany(capacityreachedtask, timeouttask);                 this.conditionreachedevent();             }              protected override void oncollectionchanged(system.collections.specialized.notifycollectionchangedeventargs e)             {                 base.oncollectionchanged(e);                 if (this.count >= this.alertthreshold)                 {                     capacityreached.trysetresult(null);                 }             }         } 

Comments