multithreading - GUI with c++. lagged when i use button -


am new in c++ gui, modifying code sent machine me, want make while loop when click button, tried thread , still stuck.

 void cdlgwriteepc::loop() { // }  void cdlgwriteepc::onbnclickedok() {     std::thread loadingthread(&cdlgwriteepc::loop, this);     loadingthread.join();    } 

join blocks current thread until other thread done, that's no help. should start worker thread, return , worker thread should send kind of message when it's done.

the function names in example code seem it's msvc++ mfc application we'll work that.

simply put, windows gui applications event driven, each time event happens wm_message sent. framework receives these messages , calls appropriate functions handle it. can define our own messages , message handlers. way worker thread can send such messages framework , call our handler function.

wm_app defined starting point private user messages, there won't conflict existing system messages.

(https://msdn.microsoft.com/en-us/library/windows/desktop/ms644930%28v=vs.85%29.aspx)

so, imagine building mfc dialog application searches in file. if file big can take long time , prevent blocking main thread , getting 'window not responding' message need in worker thread. might want progress bar example.

first, define our own messages inside our existing dialog class starting wm_app + 1 , add our handler functions, these must of following type: afx_msg lresult (cwnd::*)(wparam, lparam)

wparam , lparam parameters passed when posting message, can use them send custom data. in our example can use them send % progress our progress bar.

(https://msdn.microsoft.com/en-us/library/k35k2bfs.aspx)

 class cmyappdlg : public cdialogex  {   public:      //public worker thread class can use these same sames when posting message.     enum messages       {         msg_threadprogress = wm_app + 1,         msg_threaddone     };    private:      afx_msg lresult onthreadprogress(wparam wparam, lparam lparam)     {         progressbar->setpos(wparam); //worker thread posts progress wparam     };      afx_msg lresult onthreaddone(wparam wparam, lparam lparam)     {         //get result worker thread class , use it...     };  }; 

then need add our messages , handlers message map, should add them existing message map in .cpp file dialog/document.

begin_message_map(cmyappdlg, cdialogex)  on_message(msg_threadprogress, &cmyappdlg::onthreadprogress)     on_message(msg_threaddone, &cmyappdlg::onthreaddone)             end_message_map() 

now can post these messages in our worker thread , framework main thread handle messages , update progress bar or use result:

class threadclass {   public:      //constructor takes reference our dialog class because need window handle     //to post message     threadclass(cmyappdlg& myappdlg) : mmyappdlg(myappdlg) {};      void operator()() //thread worker code...     {         while (what_we_look_for_not_found)         {             int progress = 0;             //search while , update progress variable...              //post message dialog asking update progressbar             postmessage(mmyappdlg.m_hwnd, cmyappdlg::msg_threadprogress, progress, 0);         }          //finished searching...         storeresult();          //post message dialog informing thread done , result can retrieved.         postmessage(mmyappdlg.m_hwnd, cmyappdlg::msg_threaddone, 0, 0);     }    private:      cmyappdlg& mmyappdlg; }; 

qt uses similar system signal , slot , other frameworks surely have own equivalent system. should able find more information in manuals if using else msvc++ mfc.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -