Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

C/C++ - SYSTEMTIME 값 기준으로 특정 시간이 지났는지를 판단하는 함수

C#으로 구현하면 이런 식인데,

static void Main(string[] args)
{
    DateTime old = DateTime.Now;
    Thread.Sleep(1000);

    if ((DateTime.Now - old).TotalMilliseconds > 1000)
    {
        Console.Write("Hello");
    }

    Console.WriteLine("World!");
}

Windows 환경의 C/C++에서 SYSTEMTIME으로 저 작업을 하려니 코드가 좀 복잡해지는데, ^^ 제 경우에는 stackoverflow의 어느 코드인가를 참고해서 다음과 같이 만들어봤습니다.

bool IsElapsedTime(SYSTEMTIME* oldTime, SYSTEMTIME* currentTime, ULONGLONG milliSeconds)
{
    union TimeUnit {
        ULARGE_INTEGER li;
        FILETIME       ft;
    };

    TimeUnit oldUnit, newUnit;

    SystemTimeToFileTime(oldTime, &oldUnit.ft);
    SystemTimeToFileTime(currentTime, &newUnit.ft);

    // Add in the seconds
    oldUnit.li.QuadPart += (milliSeconds * 10000); // 1 milli == 1000000 nano
                                                   // 1 milli == 10000 * 100 nano

    return oldUnit.li.QuadPart <= newUnit.li.QuadPart;
}

SYSTEMTIME을 FileTime으로 바꾸면,

SYSTEMTIME structure
; https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime

FILETIME structure
; https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime

QuadPart에 1이 있는 경우 그 값이 100 나노초가 되므로 밀리 초를 나타내기 위해 10,000을 곱하는 식으로 해결하면 됩니다.

어쨌든 사용법은, 요런 식으로 할 수 있습니다.

if (DateTimeExtension::IsElapsedTime(&oldTime, &currentTime, 500) == true)
{
    // 500ms 지난 후 해야 할 작업 수행
}




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/7/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13787정성태10/26/20247106개발 환경 구성: 730. github - Linux 커널 repo를 윈도우 환경에서 git clone하는 방법 [1]
13786정성태10/26/20246770Windows: 266. Windows - 대소문자 구분이 가능한 파일 시스템
13785정성태10/23/20246024C/C++: 182. 윈도우가 운영하는 2개의 Code Page파일 다운로드1
13784정성태10/23/20246726Linux: 95. eBPF - kprobe를 이용한 트레이스
13783정성태10/23/20246222Linux: 94. eBPF - vmlinux.h 헤더 포함하는 방법 (bpf2go에서 사용)
13782정성태10/23/20245809Linux: 93. Ubuntu 22.04 - 커널 이미지로부터 커널 함수 역어셈블
13781정성태10/22/20246414오류 유형: 930. WSL + eBPF: modprobe: FATAL: Module kheaders not found in directory
13780정성태10/22/20247640Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블
13779정성태10/22/20245945개발 환경 구성: 729. WSL 2 - Mariner VM 커널 이미지 업데이트 방법
13778정성태10/21/20247794C/C++: 181. C/C++ - 소스코드 파일의 인코딩, 바이너리 모듈 상태의 인코딩
13777정성태10/20/20246130Windows: 265. Win32 API의 W(유니코드) 버전은 UCS-2일까요? UTF-16 인코딩일까요?
13776정성태10/19/20247147C/C++: 180. C++ - 고수준 FILE I/O 함수에서의 Unicode stream 모드(_O_WTEXT, _O_U16TEXT, _O_U8TEXT)파일 다운로드1
13775정성태10/19/20247165개발 환경 구성: 728. 윈도우 환경의 개발자를 위한 UTF-8 환경 설정
13774정성태10/18/20246695Linux: 91. Container 환경에서 출력하는 eBPF bpf_get_current_pid_tgid의 pid가 존재하지 않는 이유
13773정성태10/18/20246402Linux: 90. pid 네임스페이스 구성으로 본 WSL 2 + docker-desktop
13772정성태10/17/20246690Linux: 89. pid 네임스페이스 구성으로 본 WSL 2 배포본의 계층 관계
13771정성태10/17/20246499Linux: 88. WSL 2 리눅스 배포본 내에서의 pid 네임스페이스 구성
13770정성태10/17/20246962Linux: 87. ps + grep 조합에서 grep 명령어를 사용한 프로세스를 출력에서 제거하는 방법
13769정성태10/15/20248276Linux: 86. Golang + bpf2go를 사용한 eBPF 기본 예제파일 다운로드1
13768정성태10/15/20247387C/C++: 179. C++ - _O_WTEXT, _O_U16TEXT, _O_U8TEXT의 Unicode stream 모드파일 다운로드2
13767정성태10/14/20246165오류 유형: 929. bpftrace 수행 시 "ERROR: Could not resolve symbol: /proc/self/exe:BEGIN_trigger"
13766정성태10/14/20245537C/C++: 178. C++ - 파일에 대한 Text 모드의 "translated" 동작파일 다운로드1
13765정성태10/12/20247111오류 유형: 928. go build 시 "package maps is not in GOROOT" 오류
13764정성태10/11/20247767Linux: 85. Ubuntu - 원하는 golang 버전 설치
13763정성태10/11/20246407Linux: 84. WSL / Ubuntu 20.04 - bpftool 설치
13762정성태10/11/20246563Linux: 83. WSL / Ubuntu 22.04 - bpftool 설치
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...