c# - Trigger an Event after x seconds, but also cancel it before it executes -


i'm developing web api (which works quite well). what's missing? here sample code of get action:

public ienumerable<xxxx> get() {                ienumerable<xxxx> yyyy = new list<xxxx>();     //get yyyy database     timer = new timer();     timer.autoreset = true;     timer.enabled = true;     timer.interval = 5000; //miliseconds     timer.elapsed += timer_elapsed;     timer.start();     return yyyy; } void timer_elapsed(object sender, elapsedeventargs e) {     //code executed when timer elapses... } 

so once request received, timer initialized , fire elapsed event @ interval of 5 seconds. on next subsequent request continues....

the expected behavior such that:

  1. initialize request -1
  2. initialize timer -1
  3. if request same client received within 5 seconds, timer must not fire elapsed event.
  4. if no request received same client within 5 seconds, timer should elapse , fire event.

also timer has nothing client(s).

here further business scenario related this.... i'm developing web api consumed electronic device when switched on. device keep sending it's on status long power available. as, user turns off switch, request server stops.

these status updated database whether device on or off. trickier part identify when device turns off (complicated because server not know if device stops sending request). each devices there separate timer.

first of all, thank @patrick hofman guide me , think out of box... implemented class having static property inside it.

public class devicecontainer {     public static list<devtimer> timers=new list<devtimer>(); } public class devtimer:timer {     public string identifier {get; set;}     public bool isinuse{get; set;} } 

and in above code (in question), made following changes:

public ienumerable<xxxx> get(string id) {             //check if timer exists in     if(!devicecontainer.timers.any(s=>s.identifier.equals(id)))     {          //create new object of timer, assign identifier =id,           //set interval , initialize it. add collection          var timer = new devtimer();          timer.autoreset = true;          timer.enabled = true;          timer.interval = 5000; //miliseconds          timer.elapsed += timer_elapsed;          timer.isinuse=true;          timer.identifier=id;                       devicecontainer.timers.add(timer);          timer.start();     }     else     {          //code stop existing timer , start again.          var _timer=devicecontainer.timers.firstordefault(s=>s.identifier.equals(id))                        devtimer;          _timer.stop();          _timer.start();     }          } void timer_elapsed(object sender, elapsedeventargs e) {     //code turn off device in db } 

i'm not posting entire code that's not purpose here.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -