LoginSignup
0
1

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > TTimer > 1秒ごとの処理 (等時性は悪い)

Last updated at Posted at 2016-06-23
動作確認
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

条件

  • 簡易実装としてTTimerを使う
  • 秒の飛びを発生させないこと

TTimerのIntervalは500msecにする (1秒間に2回のトライ)。
Timer1Timerの処理で秒の値をチェックする以下のように実装する。

Unit1.cpp
#include <DateUtils.hpp>

// 中略

void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
    static WORD preSec = 60;

    WORD curSec = SecondOf(Now());

    if (curSec == preSec) {
        return;
    }

    preSec = curSec;

    String msg = DateTimeToStr(Now());
    msg = msg + L"," + MilliSecondOf(Now());
    Memo1->Lines->Add(msg);
}
結果
2016/06/23 15:34:38,357
2016/06/23 15:34:39,373
2016/06/23 15:34:40,386
2016/06/23 15:34:41,401
2016/06/23 15:34:42,415
2016/06/23 15:34:43,427
2016/06/23 15:34:44,442
2016/06/23 15:34:45,455
2016/06/23 15:34:46,469
2016/06/23 15:34:47,483
2016/06/23 15:34:48,498
2016/06/23 15:34:49,5
2016/06/23 15:34:50,13
2016/06/23 15:34:51,26
2016/06/23 15:34:52,41
2016/06/23 15:34:53,62
2016/06/23 15:34:54,69
2016/06/23 15:34:55,85
2016/06/23 15:34:56,100
2016/06/23 15:34:57,109
2016/06/23 15:34:58,139
2016/06/23 15:34:59,153

右側のミリ秒の表示を見ると分かるように、かなり等時性の精度は悪い。ときどき500mse程度の間隔になる (15:34:49の処理)。

前回の処理から今回の処理を常に1000msec ± 100msecなどで実行したい場合は別の実装(TThreadを使うなど)が必要。

Timer1->Enable = falseしてからtrueにした時の整合性についてはきちんとはしていない (static宣言)。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1