흔히 서버어플을 만들다 보면 GettickCount를 사용하는 코드들이 있는데
장시간 운영중인 운영체제에서는 해당함수가 -1을 리턴하는경우가 있다.
Delphi2010은 Cardinal 값으로 반환 받는데 cardinal이라면 약 49 일이후에는 GettickCount가
엉뚱한 값을 내뱉는 경우가 발생하여 코드에 따라 예기치 않은 동작을 보이거나 심지어
Access Violation까지 뱉어내기도 한다.
아래 코드를 이용하면 GetTickCount64 함수를 이용할 수 있는데
무려 약 2억9천2백4십7만천2백8년째 가동중인것까지 사용이 가능하다.
출처 : http://www.delphipraxis.net/711170-post5.html
Unit GTCUnit;
// (c) 1997-2007 by FNS Enterprize's (FNSE.de)
// 2003-2007 by himitsu @ Delphi-PRAXiS.de
Interface
Uses Windows;
Var GetTickCount64: Function(): UInt64; StdCall;
Implementation
Function _GetTickCount64: UInt64; StdCall;
Var Freq, Ticks: UInt64;
Begin
If QueryPerformanceFrequency(Int64(Freq))
and QueryPerformanceCounter(Int64(Ticks))
and (Int64(Freq) > 0) Then Begin
If Ticks >= UInt64(-1) div 1000 Then Result := Ticks div (Freq div 1000)
Else Result := (Ticks * 1000) div Freq;
End Else Result := 0;
End;
Initialization
GetTickCount64 := GetProcAddress(GetModuleHandle('Kernel32.dll'), 'GetTickCount64');
If @GetTickCount64 = nil Then GetTickCount64 := @_GetTickCount64;
End.
사용예
Uses Windows, GTCUnit;
.....
Initialization
GetTickCount64 := GetProcAddress(GetModuleHandle('Kernel32.dll'), 'GetTickCount64');
If @GetTickCount64 = nil Then GetTickCount64 := @_GetTickCount64;
End.
사용예
Uses Windows, GTCUnit;
.....
....
Var C, C2: UInt64;
Begin
C := GetTickCount64();
Sleep(3000);
C2 := GetTickCount64();
Label1.Caption := IntToStr(C2 - C);
End;
Begin
C := GetTickCount64();
Sleep(3000);
C2 := GetTickCount64();
Label1.Caption := IntToStr(C2 - C);
End;
댓글 없음:
댓글 쓰기