windows - Alternative to TControl.Perform -
the tcontrol.perform code is:
var message: tmessage; begin message.msg := msg; message.wparam := wparam; message.lparam := lparam; message.result := 0; if self <> nil windowproc(message); result := message.result; the program execution awaits return, right?
there alternative, posting message in tform queue, inside thread, in same application, without waiting return?
edit
this approach mitigate problem?
interface const wm_dostuff = wm_app + $001; tmyform = class(tform) {stuff} public {other stuff} procedure domystuff(var msg: tmessage); message wm_dostuff; {more stuff} end; var myhandle: hwnd; implementation constructor tmyform.create(aowner: tcomponent); begin inherited; myhandle := allocatehwnd(domystuff); end; destructor tmyform.destroy; begin deallocatehwnd(myhandle); inherited; end; and use inside thread:
postmessage(myhandle, wm_dostuff, 0, 0);
to add message queue of thread associated window, need use postmessage windows api function.
postmessage(windowhandle, msg, wparam, lparam); now, if doing on different thread gui thread, cannot use form.handle obtain window handle. that's because doing introduces race gui thread. , if handle needs re-created, created affinity thread rather gui thread. remember rules: interact vcl objects gui thread.
so typically not use postmessage handle of vcl form because cannot guarantee message delivered correct window. if synchronize access window handle, window can re-created , message not arrive.
the simplest way deliver messages asynchronously call tthread.queue. not need window handle operate , avoids issues vcl object affinity gui thread. procedure send when call queue executes on gui thread , safe perform vcl operations.
if on older delphi pre-dates tthread.queue more complicated. should in case use postmessage. you'll have direct message window not associated form. direct window created allocatehwnd. remember must call allocatehwnd on gui thread. windows created way immune re-creation , safe targets postmessage. window procedure window can forward message on form. , safe because window procedure executes in thread associated window. in case gui thread.
as aside, if calling tcontrol.perform away gui thread, wrong. expect intermittent , hard diagnose failures.
Comments
Post a Comment