//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
//---------------------------------------------------------------------------
void _stdcall TimerProc(HWND Window, UINT Message, UINT idEvent, DWORD dwTime)
{
printf("Timer triggered !!\n");
}
#pragma argsused
int main(int argc, char* argv[])
{
// ミ璸竟
int TimerID = SetTimer(0, 0, 1000, (TIMERPROC)&TimerProc);
// 眔癟, Μ WM_QUIT 铬瞒癹伴
TMsg Msg;
while (GetMessage(&Msg, 0, 0, 0))
DispatchMessage(&Msg);
getchar(); // 单传︽龄
// 篟反璸竟
KillTimer(0, TimerID);
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <mmsystem.hpp>
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int TriggerCount = 0;
// ミ event object: automatic reset, initial non-signaled state
HANDLE hEvent = CreateEvent(NULL, false, false, NULL);
// 砞ミ碈砰璸竟, 猔種篨夹砞﹚
int TimerID = timeSetEvent(1000, 0, (LPTIMECALLBACK)hEvent, 0,
TIME_PERIODIC | TIME_CALLBACK_EVENT_SET);
do {
// 单 hEvent 砆砞﹚ signaled
WaitForSingleObject(hEvent, INFINITE);
printf("Timer Triggered: #%d\n", TriggerCount);
TriggerCount++;
} while (TriggerCount <= 10);
timeKillEvent(TimerID);
CloseHandle(hEvent);
printf("Press anykey to exit ...");
getchar();
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnFreqClick(TObject *Sender)
{
TLargeInteger L;
if (QueryPerformanceFrequency(&L))
ShowMessage("Frequency: " + IntToStr(L.QuadPart));
else
ShowMessage("This system doesn't support high-resolution performance counter !!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCounterClick(TObject *Sender)
{
TLargeInteger L;
if (QueryPerformanceCounter(&L))
ShowMessage("Counter: " + IntToStr(L.QuadPart));
else
ShowMessage("This system doesn't support high-resolution performance counter !!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
TLargeInteger Start, L;
int Freq;
QueryPerformanceFrequency(&L);
Freq = L.QuadPart / 1000;
bStopCounter = false;
QueryPerformanceCounter(&Start);
do {
QueryPerformanceCounter(&L);
lblElapseTime->Caption = Format("Elpased time: %d ms",
OPENARRAY(TVarRec, ((int)(L.QuadPart - Start.QuadPart) / Freq)));
Application->ProcessMessages();
} while (!bStopCounter && !Application->Terminated);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStopClick(TObject *Sender)
{
bStopCounter = true;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <mmsystem.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
DWORD TimerInterval = 10; // 箇砞丁筳丁 10 睝
int MMTimerID; // 纗 MMTimer ID
DWORD StartTime; // 币笆璸竟パ GetTickCount 眔丁
DWORD TriggerCount; // 牟祇Ω计
DWORD StopCount; // 璝ぃ 0, 牟祇Ω计笷笆氨ゎ
void _stdcall TimerProc(HWND Window, UINT message, UINT idEvent, DWORD dwTime)
{
Form1->CommonTimerProc();
}
void _stdcall MMTimerProc(UINT uTimerID, UINT uMessage, DWORD dwUser, DWORD dw1, DWORD dw2)
{
Form1->CommonTimerProc();
}
void __fastcall TForm1::WMTimer(TWMTimer& message)
{
CommonTimerProc();
}
void TForm1::CommonTimerProc()
{
DWORD TimeDiff; // 龟悔禣丁
// 璸衡龟悔禣丁
TimeDiff = GetTickCount() - StartTime;
lblElapsed->Caption = "Time : " + IntToStr(TimeDiff) + " ms";
// 仓璸计竟
TriggerCount++;
lblCount->Caption = "Count: " + IntToStr(TriggerCount) + " times";
// 粇畉 = 龟悔龟悔禣丁 - 璸计竟 * 丁筳丁
lblError->Caption = "Error: " + IntToStr(TimeDiff - TriggerCount *
TimerInterval) + " ms";
if (StopCount != 0 && TriggerCount == StopCount)
btnStopClick(btnStop);
}
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
TimerInterval = StrToIntDef(txtInterval->Text, 10);
StopCount = StrToIntDef(txtStopCount->Text, 0);
rgpTimerType->Enabled = false;
TriggerCount = 0;
StartTime = GetTickCount();
// 沮匡拒玻ネぃ璸竟
switch (rgpTimerType->ItemIndex) {
case 0: SetTimer(Handle, 1, TimerInterval, NULL);
break;
case 1: SetTimer(Handle, 1, TimerInterval, (TIMERPROC)&TimerProc);
break;
case 2: MMTimerID = timeSetEvent(TimerInterval, 0, (LPTIMECALLBACK)&MMTimerProc, 0,
TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
break;
}
btnStop->Enabled = true;
}
void __fastcall TForm1::btnStopClick(TObject *Sender)
{
switch (rgpTimerType->ItemIndex) {
case 0: case 1: KillTimer(Handle, 1);
break;
case 2: if (MMTimerID != 0)
timeKillEvent(MMTimerID);
}
rgpTimerType->Enabled = true;
btnStop->Enabled = false;
}
//---------------------------------------------------------------------------