timer is not panctual

hi!

i have a problem with timer because it is not panctual :S i have set interval to 10ms. that is the code in timer_tick
----------------------------------------------------------------------------------------------

private: System::Void CelotenCas_Tick(System::Object^  sender, System::EventArgs^  e) {
             this->PreverjanjeZacetka->Stop();
             this->CMeritve->Milisekunde=this->CMeritve->Milisekunde+10;
             this->VCasi->SkupneMilisekunde+=10; //skupne milisekunde
              if (this->CMeritve->Milisekunde==1000)
              {
                  this->CMeritve->Sekunde++; //ce mamo dost sekund
                 // this->VCasi->SkupneSekunde++; //celoten cas v sekundah
                  this->CMeritve->Milisekunde=0;  //dodamo minuta
              } //pa damo sekunde na 0, velja za msec in sec
              if (this->CMeritve->Sekunde==60)
              {
                  this->CMeritve->Minute++; //PRETVARJANJE IZ SEKUND V MINUTE PA ...
                  this->CMeritve->Sekunde=0;
              }
              this->VCasi->VmesneVrednistiIN=this->LPT->Lpt_Read(888+1);
              if (!this->VmesniZacetek)
              {
              if (this->VCasi->VmesneVrednistiIN==254)
              {  this->VmesniZacetek=true;
                 this->VmesniCas->Start();
                     this->LVmesniCas1->Text=this->CMeritve->Minute.ToString()+":"+this->CMeritve->Sekunde.ToString()+":"+this->CMeritve->Milisekunde.ToString();
              }}
             // this->CMeritve->KonecMeritve=this->LPT->Lpt_Read(LPTOUT+1);

              //Vm,ESNI CAS
              this->VCasi->VmesniCas2=this->LPT->Lpt_Read(888+1);
             if(!this->VmesniKonec)
              {
              if (this->VCasi->VmesniCas2==110)
              {    
                  this->VmesniKonec=true;
                    this->VmesniCas->Stop();
                    this->VCasi->seEneMilisekunde=System::Convert::ToDouble(this->VCasi->VmesneMilisekunde);
                    this->VCasi->seEneMilisekunde*=0.001;
                    this->VCasi->PomozneSekunde+=this->VCasi->seEneMilisekunde;
                    this->neki=this->DolzonaRavnine->Text->ToString();
                    this->VCasi->DovzinaRavninePOm=System::Convert::ToInt32(neki);
                    this->VCasi->DovzinaRavnineVM=this->VCasi->DovzinaRavninePOm*0.001;
                    this->VCasi->MaxSpeed=this->VCasi->DovzinaRavnineVM/this->VCasi->PomozneSekunde;
                    this->VCasi->MaxSpeed=this->ShraniVFile->Zaokrozi(this->VCasi->MaxSpeed,3);
                    // this->VCasi->MaxSpeed/=0.001;
                    this->LCelotenSpeed->Text=this->VCasi->MaxSpeed.ToString();   
                    this->LVmesniCas2->Text=this->CMeritve->Minute.ToString()+":"+this->CMeritve->Sekunde.ToString()+":"+this->CMeritve->Milisekunde.ToString();
              }
              }
              this->VCasi->koncniCas=this->LPT->Lpt_Read(888+1);
                //merjenje SPEEEDA POVPRECNEGA
                    if (this->VCasi->koncniCas==94)
                    {
                        this->CelotenCas->Stop();
                        //this->PreverjanjeZacetka->Start();
                        this->VCasi->SkupneMilisekunde*=0.001; //da dobimo sekunde
                        this->seneki=this->DolzinaLabirinta->Text->ToString();
                        this->VCasi->CelotnaDovzina=System::Convert::ToDouble(this->seneki); //dovzina labirinta v metrih
                        this->VCasi->PovprecenSpeed=this->VCasi->CelotnaDovzina/this->VCasi->SkupneMilisekunde;
                        //this->VCasi->PovprecenSpeed*=0.1;
                        this->VCasi->PovprecenSpeed=this->ShraniVFile->Zaokrozi(this->VCasi->PovprecenSpeed,3);
                        this->label11->Text=this->VCasi->PovprecenSpeed.ToString();
                      

                    }

              //zadevo e izpi emo na zaslon
              this->label1->Text=this->CMeritve->Minute.ToString()+":"+this->CMeritve->Sekunde.ToString()+":"+this->CMeritve->Milisekunde.ToString();

         }


 

--------------------------------------------------------------------------------------------
i am counting ms and converting them to secounds and minutes and then writeing them to label but timer is not panctual :S i am not getting right time :S
any idea what should be wrong :S



Answer this question

timer is not panctual

  • dwhsix

    Hi,

    For higher resolution you should use the multi-media timers. See the following link for details:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/multimed/htm/_win32_multimedia_timers.asp

    Ronald Laeremans
    Visual C++ team

  • FavorFlave

    Timers aren't very precise, especially for such a small interval (10 ms). Moreover it seems that you are using the System.Windows.Forms.Timer which has an "accuracy ... limited to 55 milliseconds" (from MSDN). I'm not sure what are you trying to achieve with your code but can't you simply use the time provided by the system in DateTime.Now instead of measuring your own time
  • PerGeert

    no i cant use systems time and date because i have some sensors pluged into LPT and i am doing some acctions when they send 1.


  • Giant_Panda

    If you're using .NET 2.0 there's also the StopWatch class which uses QueryPerformanceCounter API internally.  It *should* give you millisecond accuracy, but it's hardward dependent.  There are static properties that you can see during debugging of watch that shows you some metrics of the timer.

    Stopwatch watch = new Stopwatch();

    The number of ticks per millisecond on my 3.0GHz Pentium 4 machine is 10,000. 

    Brian

  • Joppe Pelarn

    oky thanx i will try :D i hope it will be panctual :D

    LP

    Rayman2


  • timer is not panctual