Problem with delay

This is a discussion on Problem with delay within the C++ Programming forums, part of the General Programming Boards category; Hello , I am beginer in c++ as i started few days ago and i find it hard to understand ...

  1. #1
    248
    248 is offline
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Problem with delay

    Hello , I am beginer in c++ as i started few days ago and i find it hard to understand most of syntax.

    What i should use to make part of code do a calculation 1 time and after that not repeat actions?
    What should i use to make part of code to repeat itself?
    I am trying to make a widely customizable delay timer of 3sec out of QueryPerformanceFreqency and QueryPerformanceCounter by first calculating QueryPerformanceFreqency*3+QueryPerformanceCounter , then not repetaing it anymore and later it should start to compare results from QueryPerformanceFreqency*3+QueryPerformanceCounter to QueryPerformanceCounter. So if value becaomes bigger it would start to do delayed task.

    program, it compiles under visual studio 2010 but i am not able to make a delay as i was hoping for because of too little understanding how to manipulate values in code.

    idea is that by pressing checkbox1 it would make aaaaaaa appear with 3sec delay.

    Code:
    #pragma once
    #include <iostream>
    #include <windows.h>                // for Windows APIs
    
    namespace timerfinal {
    
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
    
        /// <summary>
        /// Summary for Form1
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
            Form1(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
    
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }
        private: System::Windows::Forms::CheckBox^  checkBox1;
        private: System::Windows::Forms::Label^  label1;
        protected: 
    
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
                this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
                this->label1 = (gcnew System::Windows::Forms::Label());
                this->SuspendLayout();
                // 
                // checkBox1
                // 
                this->checkBox1->AutoSize = true;
                this->checkBox1->Location = System::Drawing::Point(44, 12);
                this->checkBox1->Name = L"checkBox1";
                this->checkBox1->Size = System::Drawing::Size(80, 17);
                this->checkBox1->TabIndex = 0;
                this->checkBox1->Text = L"checkBox1";
                this->checkBox1->UseVisualStyleBackColor = true;
                this->checkBox1->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox1_CheckedChanged);
                // 
                // label1
                // 
                this->label1->AutoSize = true;
                this->label1->Location = System::Drawing::Point(41, 32);
                this->label1->Name = L"label1";
                this->label1->Size = System::Drawing::Size(35, 13);
                this->label1->TabIndex = 1;
                this->label1->Text = L"label1";
                // 
                // Form1
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(158, 77);
                this->Controls->Add(this->label1);
                this->Controls->Add(this->checkBox1);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->ResumeLayout(false);
                this->PerformLayout();
    
            }
    #pragma endregion
        private: System::Void checkBox1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
    
    
    
    
    
    
    {
        LARGE_INTEGER freq;            // ticks per second
        LARGE_INTEGER time;            //count+needed time   
        LARGE_INTEGER count;           //all ticks
    
          // get ticks per second
        QueryPerformanceFrequency(&freq);
          // count for ending
        QueryPerformanceCounter(&count);
          //ticks needed
        time.QuadPart = (freq.QuadPart*3) + count.QuadPart;      
        
        while (count.QuadPart >= time.QuadPart){             
        label1->Text = "aaaaaaaaa";
                }
    }
    
    
    
    
    
    
    
    
    
                 }
        };
    }
    problem is in this part, i was hoping it to work as delay.

    as far as i am able to understrand
    QueryPerformanceFrequency should mean how many numbers per sec
    QueryPerformanceCounter should be how many numbers have passed


    i am not sure how to make it compare (count.QuadPart >= time.QuadPart) so if value becomes equal or bigger it would do task.

    time.QuadPart = (freq.QuadPart*3) + count.QuadPart;
    this calculation may only be needed once , i am not sure but maybe this repeats itself with my current code.

    Sleep() freezes all, tryed to understand others as well but too complicated to understand.
    Code:
    {
        LARGE_INTEGER freq;            // ticks per second
        LARGE_INTEGER time;            //count+needed time   
        LARGE_INTEGER count;           //all ticks
    
          // get ticks per second
        QueryPerformanceFrequency(&freq);
          // count for ending
        QueryPerformanceCounter(&count);
          //ticks needed
        time.QuadPart = (freq.QuadPart*3) + count.QuadPart;      
        
        while (count.QuadPart >= time.QuadPart){             
        label1->Text = "aaaaaaaaa";
                }
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    For a three second delay your better of not being so presice and just useing Sleep(3000).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    If you want to do .NET development, skip the (bastardized) C++ and just use C#. You'll have a much better go of it.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Location
    Michigan
    Posts
    6
    If you weren't doing in in dot net I could show you.
    but maybe... Just a delay for three seconds under c++ I would right...
    Code:
    #include <tchar.h>
    #include <Windows.h>
    
    class CMyTimer
    {
       public:
        BOOL init();
        VOID reset();
        DOUBLE getMsEllapsed();
        private:
       LARGE_INTEGER m_liOldTime;
       LARGE_INTEGER m_liCurrTime;
       LARGE_INTEGER m_liFrequency;
     };
    
    BOOL CMyTimer::init()
    {
       if(!QueryPerformanceFrequency(&m_liFrequency))
          return FALSE
        reset();
      return TRUE;
    }
    
    VOID CMyTimer::reset()
    {
        QueryPerformanceCounter(&m_liCurrTime);
        m_liOldTime = m_liCurrTime;
    }
    
    DOUBLE CMyTimer::getMsEllapsed()
    {
       m_liOldTime = m_liCurrTime;
     
       QueryPerformanceCounter(&m_liCurrTime);
      return (DOUBLE)(m_liCurrTime.QuadPart-m_liOldTime.QuadPart)/m_liFrequency.QuadPart;
    }
    
    int _tmain(void)
    {
       BOOL bThreeSeconds = FALSE;
       CMyTimer m_timer;
       if(!m_timer.init())
        {
          wcout << L"performance timer fail.\n";
          return 1;
        }
    
       DOUBLE msEllapsed = 0;
       m_timer.reset();    //just noticed this might be redundant lol
       
       while(!bThreeSeconds)
        {
           msEllapsed += m_timer.getMsEllapsed();
           if(msEllapsed >= 3)bThreeSeconds = TRUE;
        }
     return 0;
    }
    Last edited by cowell_theodore; 04-22-2012 at 09:07 PM. Reason: I didn't read over until after I submitted, oops

  5. #5
    Registered User
    Join Date
    Apr 2012
    Location
    Michigan
    Posts
    6
    Also if it were me I would have a separate thread do this so you don't freeze the program for 3 seconds. But I don't know hardly nothing about programming in dot net, Primarily because I'm already comfortable programming windows using strictly c++ without MFC, been doing it for 12 years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help.....!!!!!how much delay i will get by using delay(1)
    By gunjansethi in forum C Programming
    Replies: 7
    Last Post: 03-23-2010, 03:08 AM
  2. how to delay
    By llinocoe in forum C Programming
    Replies: 1
    Last Post: 09-22-2008, 06:09 AM
  3. I don't see a delay() in c++!
    By Makoy in forum C++ Programming
    Replies: 16
    Last Post: 12-25-2004, 07:21 AM
  4. delay() in Bc++ 5.5
    By Chiki Chiki Chalem in forum C++ Programming
    Replies: 1
    Last Post: 07-07-2002, 09:58 AM
  5. Delay
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2002, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21