CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: delay

Threaded View

  1. #1
    Join Date
    Apr 2012
    Posts
    25

    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";
                }
    }
    Last edited by 1248; April 22nd, 2012 at 12:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured