c++ - Thread execution "stops" when main form button is clicked -


i use borland c++builder 6. have app, form. app/main form kicks off thread. (tthread) thread creates new instance of server socket, , listens data. when data comes in, thread displays info on main form using synchronize method.

problem is, while thread sends info, if menu option on main form clicked, thread execution temporary halted. if comment out form1->memo1->lines->add(mstr) in synchronize method, i.e. thread not sending info main form, thread continues executing without problem. data received , responded correctly.

as restore line, , write data main form, , menu option selected on main form, thread temporary halted. there way stop behaviour thread never "blocked", still can report main form?

after reading martin's response, here did:

in main.h:

#define wm_addlog (wm_user+0x0500)  class tform1: public tform { ... private: void __fastcall virtual handleaddlog(tmessage &msg); ... public: hwnd hwnd; tstringlist *fstringbuf; __property tstringlist *stringbuf={read=fstringbuf,write=fstringbuf}; tcriticalsection tcsmsg; ... protected: begin_message_map  message_handler(wm_addlog,tmessage,handleaddlog) end_message_map(tform) } 

in main.cpp:

in tform1 constructor: ...   hwnd=findwindow(null,"softien");   if(!hwnd)   {     exit(0);   }   fstringbuf = new tstringlist;   tcsmsg = new tcriticalsection; ...  void __fastcall tform1::handleaddlog(tmessage &msg) {   string strn,strdatetime,strline;   if(memo1->lines->count>10000)     memo1->lines->clear();   while(fstringbuf->count)   {     strdatetime = "";     datetimetostring(strdatetime, "yy/mm/dd hh:nn:ss.zzz: ", now());     strn=fstringbuf->strings[0];     fstringbuf->delete(0);     strline=strdatetime + strn;     memo1->lines->add(strline);   }   tform::dispatch(&msg); }  in thread.cpp ...   m_strmsg="some message";   addlog(); ...  void __fastcall tienserverthread::addlog() {   form1->tcsmsg->acquire();   form1->stringbuf->add(m_strmsg);   form1->tcsmsg->release();   sendmessage(form1->hwnd,wm_addlog,0, 0); } 

i tried postmessage in addlog function.

everything works fine, messages written memo, app still "freezes" when click on main form menu. other ideas/help/examples?

thanks far!

i've seen before delphi tthread.synchronize, 25 years ago. when investigated see how worked, stopped using synchronize() , haven't used since, (likewise tthread.waitfor , tthread.onterminate).

use postmessage(). more effort synchronize(), requiring dedicated 'tthreadcomms' class carry data, (create in thread, load data, postmessage reference in lparam, cast in user-defined message-handler, display data, free reference), still works while modal menu options etc. popped up.

there issue postmessage(). there small number of windows operations can recreate windows in app, changing form handle. if message posted directly form handle, (the easiest way post message handler), there small, non-zero, possibility os may recreate window during operation, changing window handle. result in postmessage fail in small window of time. can avoided, mean yet more complication. can create invisible window registerclass() , createwindow() api's , post thread messages window, sending data in lparam , form/control reference in wparam. in wndproc, cast wparam tcontrol , call tcontrol.perform() call desired message-handler. need 1 of these invisible windows in app. in delphi, it's easy put stuff in dedicated unit , window create stuff in initialization section - not sure c++ builder - have initialization sections?

it more difficult use postmessage but, in contrast synchronize(), works reliably no matter main ui thread has popped up, not block secondary thread, has not been redesigned 3 times in attempt make work , core windows api not going away or changing anytime ever - code wrote in d3 still works in d2009.

rgds, martin


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -