c# - How to stop Timer Tick from spamming? -


i trying implement idle detection thread: hide mouse cursor after idle time

i have made little change it, having problem spamming test messagebox.

void activityworker_tick(object sender, eventargs e)     {         bool checkidle = (user32interop.getlastinput() > activitythreshold);          if (isidle != checkidle)         {             if (checkidle && (datetime.now.subtract(idlechecktimestamp) > timespan.fromminutes(1)))             {                 system.windows.forms.messagebox.show("idle: " + count);                 idlechecktimestamp = datetime.now;                 count++;             }              isidle = checkidle;         }     } 

each time event fires,the messagebox displayed, nothing after runs, making seem impossible stop spamming. not reset timestamp, nor increment count variable, spams messagebox "idle: 0".

can point me in correct direction on make stop spamming? want system checking idle time, want code fire once every min. or if infact idle.

you can keeping variable , storing when last sent message, , can check whether 1 minute has passed , again can show message. example:

datetime lastmessagesenton; //initialize in constructor or anywhere else before using.  void activityworker_tick(object sender, eventargs e) {     bool checkidle = (user32interop.getlastinput() > activitythreshold);      if (isidle != checkidle)     {         if (checkidle && lastmessagesenton.addmintues(1)<datetime.now)         {             system.windows.forms.messagebox.show("idle: " + count);             lastmessagesenton = datetime.now;             count++;         }          isidle = checkidle;     } } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -