マルチスレッド、コールバック、win32 api関数、およびその他の厄介な問題をいじりながら、アイデアイベントを受け取りました。(へへへ)
グローバル(またはクラスを設計するときに静的)コールバック関数を定義する代わりに、ウィンドウクラスを登録するときに割り当てDefWindowProcてからlpfnWndProc、イベントループ全体を別のスレッドで実行した場合はどうなりますか?
このように、クラスにコールバックを実装するthisときに問題をハックする必要はなく
、メインスレッドの実行が続行され、神に見捨てられたwhileループから解放され、別のウィンドウを開くこともできます(イェーイ!)
「通常の」方法:
LRESULT CALLBACK WndProc(...)
{
... // process event information
return DefWindowProc(...);
}
int CALLBACK WinMain(...)
{
... // initialize whatever needs initializing :)
WNDCLASSEX wc;
...
wc.lpfnWndProc = WndProc;
... // register the class, create the window, etc...
MSG msg;
while(GetMessage(&msg, 0, 0, 0) != 0)
{
... // TranslateMessage(&msg) if you want/need it
DispatchMessage(&msg); // dispatches the message to WndProc
}
return static_cast<int>(msg.wParam);
}
私の新しく見つけた素晴らしい方法:
DWORD WINAPI MyAwesomeEventLoop(void* data) // must be static in a class
{
... // do whatever you need with the data
MSG msg;
while(GetMessage(&msg, 0, 0, 0) != 0)
{
... // TranslateMessage(&msg) if you want/need it
... // process event information
// call PostQuitMessage(0) to leave the loop
}
return static_cast<DWORD>(msg.wParam);
}
int CALLBACK WndProc(...)
{
...
WNDCLASSEX wc;
...
wc.lpfnWndProc = DefWindowProc;
...
HANDLE threadHandle = 0;
// use "this" as the 4th parameter when implementing in a class
threadHandle = CreateThread(0, 0, MyAwesomeEventLoop, 0, 0, 0);
... // you are now free to do whatever you want! :)
// waits untill the thread finishes
// hopefully because PostQuitMessage(0) was called
WaitForSingleObject(threadHandle, INFINITE);
DWORD returnValue = 0;
GetExitCodeThread(threadHandle, &returnValue);
CloseHandle(threadHandle);
...
return static_cast<int>(returnValue);
}
皆さんはどう思いますか?