Skip to content

Instantly share code, notes, and snippets.

@wyyqyl
Created April 10, 2014 07:59
Show Gist options
  • Save wyyqyl/10353869 to your computer and use it in GitHub Desktop.
Save wyyqyl/10353869 to your computer and use it in GitHub Desktop.
Integrate boost::thread with Windows MSG
#include <windows.h>
#include <boost/thread/thread.hpp>
LRESULT MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT result = 0;
switch (message) {
case WM_TIMER:
std::cout << "WM_TIMER" << std::endl;
result = 1;
break;
default:
break;
}
return result;
}
void ThreadFunc() {
MSG msg;
SetTimer(0, 0, 1000, 0);
while (GetMessage(&msg, 0, 0, 0)) {
MsgProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
}
}
int main() {
boost::thread t(ThreadFunc);
DWORD id = GetThreadId(t.native_handle());
boost::this_thread::sleep(boost::posix_time::millisec(6000));
PostThreadMessage(id, WM_QUIT, 0, 0);
t.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment