c++ - PeekMessage inside thread -


i'm bit confused. i'm trying - create a window messages loop inside different thread. code looks this:

//... #include <thread> //...  void myclass::runmainloop() {     new thread(mainloop, this); //i know cause memory leak - testing }  void myclass::mainloop(myclass* _this) { // <- static method     cout << "start thread" << endl; //loop function started     msg msg;     while (true) {         while (peekmessage(&msg, _this->_hwnd, 0, 0, pm_remove)) {             cout << msg.message << " "; //we've got message! (don't here inside thread)             if (msg.message == wm_quit) {                 cout << "exiting" << endl; //closing window                 break;             }             dispatchmessage(&msg);         }          sleep(2);     } } 

the message "start thread" appears, don't see messages handled. on other hand, when call mainloop() method without creating thread, seems work fine:

void myclass::runmainloop() {     mainloop(this); } 

i tryed dig msdn, didn't find on problem. seems, have gaps in knowledge, can't filled in reasonable time.

my thought thread somehow "don't know" window i've created inside main program thread.

so, question - doing wrong? why message loop isn't working in thread?

edit:

the code creates window. runs in program's main thread.

wndclassex            wcx; pixelformatdescriptor pfd; rect                  rect; hglrc                 hrctemp; dword                 style, exstyle; int                   x, y, format;  _hinstance = (hinstance)getmodulehandle(null);  //register window class memset(&wcx, 0, sizeof(wcx)); wcx.cbsize        = sizeof(wcx); wcx.style         = cs_hredraw | cs_vredraw | cs_owndc; wcx.lpfnwndproc   = (wndproc)windowproc; wcx.hinstance     = _hinstance; wcx.lpszclassname = l"windowclassname"; wcx.hicon         = loadicon(null, idi_application); wcx.hcursor       = loadcursor(null, idc_arrow);  registerclassex(&wcx)  //window styles style   = ws_caption | ws_sysmenu | ws_minimizebox; exstyle = ws_ex_appwindow;  //place window @ cetner of screen x = (getsystemmetrics(sm_cxscreen) - width)  / 2; y = (getsystemmetrics(sm_cyscreen) - height) / 2;  rect.left   = x; rect.right  = x + width; rect.top    = y; rect.bottom = y + height;  //adjust  window size styles adjustwindowrectex(&rect, style, false, exstyle);  //create window _hwnd = createwindowex(exstyle, wcx.lpszclassname, l"window caption", style, rect.left, rect.top,     rect.right - rect.left, rect.bottom - rect.top, null, null, _hinstance, null); 

edit 2:

thanks commented question, understand problem is: the thread created window receives messages it.

so, i'll rephrase question: can redirect window messages thread? tried attachthreadinput, didn't succeed.

an hwnd tied thread context created in. thread context creates hwnd can receive messages hwnd. when call mainloop() directly in runmainloop(), mainloop() running in same thread context created hwnd, why works. once move mainloop() different thread, can no longer receive messages hwnd.

an hwnd's message pump must in same thread created hwnd. there no getting around limitation. if want service hwnd in different thread have create hwnd in thread.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -