c# - Can I make this TimerQueueTimer class any more performant? -


using system; using system.threading;  internal class timerqueuetimer : idisposable {     public timerqueuetimer(int interval, int msbeforefirstcall)     {         this.interval = interval;         this.msbeforefirstcall = msbeforefirstcall;         this.callback = this.ticked;         this.isthefirsttick = true;         this.isstopped = true;     }      public event eventhandler ticked;      public void start()     {         if (!this.isstopped)         {             return;         }          this.isthefirsttick = true;         this.isstopped = false;         computer.changetimerresolutionto(1);         nativemethods.createtimerqueuetimer(             out this.handle,             intptr.zero,             this.callback,             intptr.zero,             (uint)this.msbeforefirstcall,             (uint)this.interval,             callbackexecution.executeintimerthread);     }      public void stop()     {         if (this.isstopped)         {             return;         }          nativemethods.deletetimerqueuetimer(             intptr.zero,             this.handle,             intptr.zero);         computer.cleartimerresolutionchangeto(1);         this.isstopped = true;     }      public void dispose()     {         this.stop();     }      private void ticked(intptr parameterpointer, bool timerorwaitfired)     {         if (this.isstopped)         {             return;         }          if (this.isthefirsttick)         {             thread.currentthread.priority = threadpriority.highest;         }          this.isthefirsttick = false;         var ticked = this.ticked;         if (ticked != null)         {             ticked(this, eventargs.empty);         }     }      private intptr handle;     private volatile bool isstopped;     private volatile bool isthefirsttick;     private readonly waitortimerdelegate callback;     private readonly int interval;     private readonly int msbeforefirstcall; } 

(note: computer.changetimerresolutionto() , computer.cleartimerresolutionchangeto() call timebeginperiod , timeendperiod, respectively.)

questions:

  1. the callback running in timer's thread, rather threadpool thread. fine long callback function fast, right?
  2. does setting callback thread (and timer thread) priority highest in terms of performance?
  3. would better make timer interval 1ms , count ticks, raising ticked if tickcount % interval == 0? lower interval timer more accurate , precise?
  4. is there reason might less accurate and/or precise created timesetevent timer?

the reason ask because running issues timer callback being delayed ~50ms when system under heavy load. compared when using timesetevent felt happened less often--though might illusion. know windows isn't deterministic, there's can do. however, want make sure i've done can make high-priority possible. there else can do?

i used priority queue solve problem: each element of queue contains callback address (the timer routine), pointer callback parameters , time in future when should fired.

the 'time' priority, logic here have possibility wake timer thread thread. when callback added queue thread timer thread waken , top element of priority queue, calculates different between current time , 'time' stored in queue , sleeps until calculated timeout exceeds.

when timer thread awaken timeout starts new thread thread pool invokes callback.

i have timer queue implementation here, not tested can see if helps.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -