CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Apr 2012
    Posts
    25

    QueryPerformanceFreqency and QueryPerformanceCounter

    hello , i try make a customizable delay out of QueryPerformanceFreqency and QueryPerformanceCounter i try is as i googled for 6+ hours and couldnot find anything to work with but i am not sure how to get it working.

    Problem for me is that i have no idea how to use QueryPerformanceCounter so that value would be updating constantly not only 1 value and finished.
    What sould i use in code to make values update itself very fast automatically?

    for example id like QueryPerformanceCounter to update its value that is compared(<=) to calculation with fixed values. QueryPerformanceFreqency+QueryPerformanceCounter*2 to get a 2sec delay as soon as value is equal or larger than other and then
    use if true to do delayed task.

    How to make QueryPerformanceCounter update itself in comaprison to calculation that would be with fixed values?
    Last edited by 1248; April 22nd, 2012 at 04:16 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    Quote Originally Posted by 1248 View Post
    hello , i try make a customizable delay out of QueryPerformanceFreqency and QueryPerformanceCounter i try is as i googled for 6+ hours and couldnot find anything to work with but i am not sure how to get it working.

    Problem for me is that i have no idea how to use QueryPerformanceCounter
    Then why do you want to implement it?
    And the fact that you "googled for 6+ hours" means nothing! Someone could find the solution after googling for a couple of minutes, another one doesn't find it during the weeks!
    The main problem with "googling" is you have to know what you are looking for!

    PS: BTW, what is wrong for you with Timers?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    If i know how to implement timers nothing is wrong.
    If not then trying to make simple things work seems be very time consuming , il stick to tourials as it may teach more than watching google logo.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    Well, again: did you read about Timers and its using in MSDN (I gave you a link in my previous post)? There are some code examples so you could very easy and fast implement it.

    As for "QueryPerformanceFreqency and QueryPerformanceCounter": I don't use them (because I just don't need to!), so cannot give you any example.
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    yes i am trying to understand those, first i will need to undersatand terms then further.

  6. #6
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    {
    LARGE_INTEGER freq; // ticks per second
    LARGE_INTEGER startcount; //ticks at beginning
    LARGE_INTEGER count; //all ticks
    __int64 Time;

    // get ticks per second
    QueryPerformanceFrequency(&freq);
    // count at start
    QueryPerformanceCounter(&startcount);
    // count for ending
    QueryPerformanceCounter(&count);
    // needed time
    Time = t2.QuadPart+freq.QuadPart*3 //ticks at beginning+1sec*3

    while(count => Time){ //time reaches higher than ticks at beginning+1sec*3

    label1->Text = "aaaaaaaaa";
    }

    what is wrong with this code?

    following errors appear:
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(114): error C2065: 't2' : undeclared identifier
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(114): error C2228: left of '.QuadPart' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(116): error C2143: syntax error : missing ';' before 'while'
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(116): error C2059: syntax error : '>'
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(116): error C2143: syntax error : missing ';' before '{'

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    Quote Originally Posted by 1248 View Post
    Code:
    {
        LARGE_INTEGER freq;        // ticks per second
        LARGE_INTEGER startcount;      //ticks at beginning
        LARGE_INTEGER count;           //all ticks
        __int64 Time;
    
        // get ticks per second
        QueryPerformanceFrequency(&freq);
        // count at start
        QueryPerformanceCounter(&startcount);
        // count for ending
        QueryPerformanceCounter(&count);
        // needed time
        Time = t2.QuadPart+freq.QuadPart*3  //ticks at beginning+1sec*3
    
        while(count => Time){               //time reaches higher than ticks at beginning+1sec*3
    
        label1->Text = "aaaaaaaaa";
                }
    what is wrong with this code?

    following errors appear:
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(114): error C2065: 't2' : undeclared identifier
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(114): error C2228: left of '.QuadPart' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(116): error C2143: syntax error : missing ';' before 'while'
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(116): error C2059: syntax error : '>'
    1>c:\users\--\documents\visual studio 2010\projects\timer\timer\Form1.h(116): error C2143: syntax error : missing ';' before '{'
    1. Please, use Code tags! Otherwise your code is not readable!
    2. The 't2' is not declared in the code snippet you have posted, So what do you expect?
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    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";
                }
    }
    Tryed to make it simple for myself and id like to ask few questions.
    Do all numbers start to update constantly or only this line?
    while (count.QuadPart > time.QuadPart)
    if all then i am giving up to waste less time of others and self as i have too little understanding to learn to timers at microsoft page that is and thisone as well.
    Is it possiblt to make it so that time.QuadPart = (freq.QuadPart * 3) + count.QuadPart; is calculated only 1 time?
    Last edited by 1248; April 22nd, 2012 at 09:38 AM.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    Quote Originally Posted by 1248 View Post
    Tryed to make it simple for myself
    So did you succeed?

    Quote Originally Posted by 1248 View Post
    Do all numbers start to update constantly or only this line?
    while (count.QuadPart > time.QuadPart)
    It doesn't make much sense to discuss non-compilable code without any requiered definitions and initializations.
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    [QUOTE=VictorN;2064873]So did you succeed?
    i belive code is now bit more simplified and it compiled and runs but if i toggle checkbox on user interface label 1 reamins label 1 not aaaaaaaaaa, i am hoping for aaaaaa to appear with delay.
    i belive that it starts to calculate both values over and over or it only does it once and stops or does nothing at all.
    What id like to learn is how to make 1 line repeat until it changes text to aaaaaaaa and other line calculate only once and use results for calculations on repeating line.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    I'm glad your code now compiles and runs...
    However, we have no idea what your "user interface" is, nor know we anything about your checkbox, "aaaaaaaaaa" nad other labels.
    Victor Nijegorodov

  12. #12
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    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";
                }
    }
    
    
    
    
    
    
    
    
    
    			 }
    	};
    }
    This compiles but i am not able to make a delay as i was hoping for because of too little understanding how to manipulate those numbers.
    idea is taht by pressing checkbox1 it would make aaaaaaa appear with 3sec 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

    Sleep() freezes all, tryed to understand others as well but too complicated to understand.
    Last edited by 1248; April 22nd, 2012 at 10:21 AM.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    The code you've just posted is not a native VC++ code. It looks more like a managed C++ which is discussed in another forum. (Managed C++ and C++/CLI Discuss Managed C++ and .NET-specific questions related to C++)
    Victor Nijegorodov

  14. #14
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    Using QueryPerformanceCounter to insert a delay is a form of polling, and polling is evil.

    Don't do it. And if you find examples based on GetTickCount or some other timer-style API, don't use them for the same reason.

    Use timers, as suggested by VictorN.

  15. #15
    Join Date
    Apr 2012
    Posts
    25

    Re: QueryPerformanceFreqency and QueryPerformanceCounter

    What form of polling means in programming?

Page 1 of 2 12 LastLast

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