delphi - How to be notified for time changes in Windows Service -
i created windowproc notified scheme time changes:
constructor tjjwscheduler.create; begin ftimechangewnd := classes.allocatehwnd(timechangewndproc); end; procedure tjjwscheduler.timechangewndproc(var msg: tmessage); var i: integer; begin case msg.msg of wm_timechange: begin // things end; end; end;
this code running within windows service. problem isn't fired when alter scheme time!
why not broadcast message (wm_timechange) isn't delivered window? there way without loop?
edit
i don't known why, hardcoded peekmessage process messages window, , comes work fine. code below solved problem:
var msg: tmsg; if peekmessage(msg, ftimechangewnd, wm_timechange, wm_timechange, pm_remove) begin translatemessage(msg); dispatchmessage(msg); end;
this workaround strange, because have others windows processing messages (by generic processmessages), 1 isn't processing messages.
the reason window not receiving wm_timechange
messages window created secondary thread.
each thread in process has own message queue. synchronous messages delivered when service message queue, ever non-queued message wm_timechange
need service secondary threads message queue in order messages delivered.
for example, @ documentation getmessage
, mutual way pull messages of queue:
the function dispatches incoming sent messages until posted message available retrieval.
the same true peekmessage
. dispatches incoming sent messages before peeking queue.
there handful of other ways sent messages dispatched, these primary ones.
now, suspect may inconvenient periodically dispatch messages secondary thread. if secondary thread nil else can sit down in traditional getmessage
, translatemessage
, dispatchmessage
loop. , of time happily block in getmessage
dispatching incoming sent messages. if secondary thread more work that's not viable option.
you running , servicing message queue on main service thread. may create more sense create listener window have affinity main service thread. creating code runs in main service thread.
note allocatehwnd
documented not thread-safe. must not phone call thread other main thread of process. so, if wish remain on secondary thread, you'll need utilize createwindow
rather allocatehwnd
. perhaps yet reason move window onto main thread.
delphi winapi service
No comments:
Post a Comment