성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C++ CreateTimerQueue, CreateTimerQueueTimer 예제 코드</h1> <p> CreateTimerQueue, CreateTimerQueueTimer Win32 API 함수가 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > CreateTimerQueue function ; <a target='tab' href='https://learn.microsoft.com/en-us/windows/win32/api/threadpoollegacyapiset/nf-threadpoollegacyapiset-createtimerqueue'>https://learn.microsoft.com/en-us/windows/win32/api/threadpoollegacyapiset/nf-threadpoollegacyapiset-createtimerqueue</a> CreateTimerQueueTimer function ; <a target='tab' href='https://learn.microsoft.com/en-us/windows/win32/api/threadpoollegacyapiset/nf-threadpoollegacyapiset-createtimerqueuetimer'>https://learn.microsoft.com/en-us/windows/win32/api/threadpoollegacyapiset/nf-threadpoollegacyapiset-createtimerqueuetimer</a> </pre> <br /> 예제 코드는 다음에서 구할 수 있는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Using Timer Queues ; <a target='tab' href='https://learn.microsoft.com/en-us/windows/win32/sync/using-timer-queues'>https://learn.microsoft.com/en-us/windows/win32/sync/using-timer-queues</a> </pre> <br /> 만족스럽지 않습니다. TimerQueue 생성하고, Timer 작업 하나 설정한 다음 곧바로 정리하는 것이 그다지 현실적인 예제로 보이지 않습니다.<br /> <br /> 실 사용 사례로 보면, 응용 프로그램 시작하면서 TimerQueue를 하나 생성해 둘 것입니다. 그 TimerQueue는 응용 프로그램이 끝나는 시점에 닫을 것이고, 응용 프로그램이 동작하는 중에는 끊임없이 시간 관련한 작업을 TimerQueue에 던지게 될 것입니다. 이를 간단하게 코딩으로 옮겨보면 다음과 같은 방식이 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > #include "stdafx.h" #include <Windows.h> using namespace std; HANDLE g_hTimerQueue; VOID CALLBACK <span style='color: blue; font-weight: bold'>timerCallback</span>(PVOID lpParam, BOOLEAN TimerOrWaitFired) { // ... } int main() { g_hTimerQueue = CreateTimerQueue(); HANDLE hCleanTimer = nullptr; while (true) { HANDLE hTimer; <span style='color: blue; font-weight: bold'>BOOL result = CreateTimerQueueTimer(&hTimer, g_hTimerQueue, timerCallback, nullptr, 100, 0, WT_EXECUTEONLYONCE);</span> Sleep(20); } DeleteTimerQueueEx(g_hTimerQueue, INVALID_HANDLE_VALUE); return 0; } </pre> <br /> 그런데, 위와 같이 하면 메모리 누수(Memory leaks)가 발생합니다. 왜냐하면 CreateTimerQueueTimer로 TimerQueue에 추가한 작업은 반드시 DeleteTimerQueueTimer를 이용해 해제해야 하기 때문입니다.<br /> <br /> (개인적으로) 해제하기 가장 좋은 장소는 바로 timerCallback이며, 따라서 다음과 같이 작업해 주면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... typedef struct TimerWorkItem { public: int workIndex = 0; HANDLE handle = nullptr; }; VOID CALLBACK timerCallback(PVOID lpParam, BOOLEAN TimerOrWaitFired) { TimerWorkItem *workItem = (TimerWorkItem *)lpParam; if (workItem == nullptr) { return; } printf("%d\n", workItem->workIndex); <span style='color: blue; font-weight: bold'>::DeleteTimerQueueTimer(g_hTimerQueue, workItem->handle, nullptr);</span> <span style='color: blue; font-weight: bold'>delete workItem;</span> } int main() { g_hTimerQueue = CreateTimerQueue(); int workIndex = 0; while (true) { <span style='color: blue; font-weight: bold'>TimerWorkItem *workItem = new TimerWorkItem();</span> workItem->workIndex = workIndex++; BOOL result = CreateTimerQueueTimer(<span style='color: blue; font-weight: bold'>&workItem->handle</span>, g_hTimerQueue, timerCallback, <span style='color: blue; font-weight: bold'>workItem</span>, 100, 0, WT_EXECUTEONLYONCE); Sleep(1); } DeleteTimerQueueEx(g_hTimerQueue, INVALID_HANDLE_VALUE); return 0; } </pre> <br /> <hr style='width: 50%' /><br /> <br /> 참고로, CreateTimerQueueTimer의 handle 반환값을 다음과 같이 처리해 보기도 했었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... VOID CALLBACK timerCallback(PVOID lpParam, BOOLEAN TimerOrWaitFired) { TimerWorkItem *workItem = (TimerWorkItem *)lpParam; if (workItem == nullptr) { return; } <span style='color: blue; font-weight: bold'>HANDLE hTimer = workItem->handle; if (hTimer == nullptr) { return; }</span> printf("%d\n", workItem->workIndex); ::DeleteTimerQueueTimer(g_hTimerQueue, hTimer, nullptr); delete workItem; } int main() { int workIndex = 0; g_hTimerQueue = CreateTimerQueue(); while (true) { <span style='color: blue; font-weight: bold'>HANDLE hTimer;</span> TimerWorkItem *workItem = new TimerWorkItem(); workItem->workIndex = workIndex++; BOOL result = CreateTimerQueueTimer(<span style='color: blue; font-weight: bold'>&hTimer</span>, g_hTimerQueue, timerCallback, workItem, 100, 0, WT_EXECUTEONLYONCE); <span style='color: blue; font-weight: bold'>if (result == TRUE) { workItem->handle = hTimer; }</span> Sleep(1); } DeleteTimerQueueEx(g_hTimerQueue, INVALID_HANDLE_VALUE); return 0; } </pre> <br /> 위와 같이 하면, CreateTimerQueueTimer 이후에 timer handle을 workItem에 보관하게 되는데 시간 차이가 발생한다고는 해도 nullptr을 조사하는 노력이 무색하게 DeleteTimerQueueTimer에서 다음과 같은 식의 예외가 가끔씩 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Unhandled exception at 0x774E5251 (ntdll.dll) in ConsoleApplication1.exe: A LIST_ENTRY has been corrupted (i.e. double remove). </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Exception thrown at 0x774E517B (ntdll.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0xDDDDDDF9. If there is a handler for this exception, the program may be safely continued. </pre> <br /> <hr style='width: 50%' /><br /> <br /> 빈번하게 TimerQueue를 생성/삭제하는 경우라면 DeleteTimerQueueEx를 할 때도 주의를 기울여야 합니다. 실제로 다음과 같은 코드를 테스트해보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... int main() { int workIndex = 0; <span style='color: blue; font-weight: bold'>while (true)</span> { workIndex = 0; g_hTimerQueue = CreateTimerQueue(); <span style='color: blue; font-weight: bold'>while (workIndex < 1500)</span> { TimerWorkItem *workItem = new TimerWorkItem(); workItem->workIndex = workIndex++; BOOL result = CreateTimerQueueTimer(&workItem->handle, g_hTimerQueue, timerCallback, workItem, 100, 0, WT_EXECUTEONLYONCE); Sleep(1); } <span style='color: blue; font-weight: bold'>DeleteTimerQueueEx(g_hTimerQueue, INVALID_HANDLE_VALUE);</span> } return 0; } </pre> <br /> DeleteTimerQueueEx에서 다음과 같은 식의 예외가 잦은 빈도로 발생합니다. <br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Unhandled exception at 0x7759C2C8 (ntdll.dll) in ConsoleApplication1.exe: 0xC000000D: An invalid parameter was passed to a service or function. </pre> <br /> 예상으로는, Timer Queue에 현재 처리 중인 작업이 있어서 (<a target='tab' href='https://learn.microsoft.com/en-us/windows/win32/api/threadpoollegacyapiset/nf-threadpoollegacyapiset-deletetimerqueueex'>하지만 문서에서는 INVALID_HANDLE_VALUE로 호출하면 모든 콜백 작업이 완료되기를 기다린다</a>고 되어 있습니다.) 그것과 충돌이 발생하는 듯합니다.<br /> <br /> 위의 예제 같은 경우에는 DeleteTimerQueueEx 호출 전 (모든 콜백이 완료될) 1초 정도의 Sleep을 주면 아무런 오류 없이 잘 실행됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... int main() { int workIndex = 0; while (true) { workIndex = 0; g_hTimerQueue = CreateTimerQueue(); while (workIndex < 1500) { TimerWorkItem *workItem = new TimerWorkItem(); workItem->workIndex = workIndex++; BOOL result = CreateTimerQueueTimer(&workItem->handle, g_hTimerQueue, timerCallback, workItem, 100, 0, WT_EXECUTEONLYONCE); Sleep(1); } <span style='color: blue; font-weight: bold'>Sleep(1000);</span> DeleteTimerQueueEx(g_hTimerQueue, INVALID_HANDLE_VALUE); } return 0; } </pre> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1075&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 예전에 CreateTimerQueueTimer가 왠지 System.Threading.Timer에 사용되었을 것 같다는 이야기를 했었는데요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Java의 ScheduledExecutorService에 대응하는 C#의 System.Threading.Timer ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/1823'>http://www.sysnet.pe.kr/2/0/1823</a> </pre> <br /> 이참에 콜 스택을 좀 확인해 보니,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ConsoleApplication2.exe!ConsoleApplication2.Program.timerFunc(object state) Line 21 C# mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown mscorlib.dll!System.Threading.TimerQueueTimer.CallCallback() Unknown mscorlib.dll!System.Threading.TimerQueueTimer.Fire() Unknown mscorlib.dll!System.Threading.TimerQueue.FireNextTimers() Unknown [Native to Managed Transition] kernel32.dll!BaseThreadInitThunk() Unknown ntdll.dll!RtlUserThreadStart() Unknown </pre> <br /> mscorlib.dll 내에 대놓고 TimerQueueTimer라는 이름이 들어 있는 걸로 봐서 맞는 것 같습니다. ^^<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1173
(왼쪽의 숫자를 입력해야 합니다.)