-
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";
}
}
-
For a three second delay your better of not being so presice and just useing Sleep(3000).
-
If you want to do .NET development, skip the (bastardized) C++ and just use C#. You'll have a much better go of it.
-
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;
}
-
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.