C++中线程及定时器的示例代码

//---------------------------------------------------------------------------

#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;
}
//---------------------------------------------------------------------------
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值