- Cookbook - Timing loops
----boundary-LibPST-iamunique-682476207_-_-
Content-type: text/plain
Hello, Robert!
CRDSUE> I am afraid that I did not have the time
CRDSUE> to follow the details of this discussion,
CRDSUE> but the topic seemed to be of potentially
CRDSUE> general utility.
CRDSUE> With the benefit of hindsight, can someone provide
CRDSUE> a simple explanation of the problem and the recommended
CRDSUE> solution leaving out the interesting twists & turns
CRDSUE> that got there - or was the final outcome that the problem
CRDSUE> does not have a satisfactory solution?
Problem: a procedure Delay is required which should return execution
not before a certain amount of time. For example:
DoSomething1;
Delay(10000);
DoSomething2;
After DoSomething1 returns execution and before DoSomething2 starts
working there should be a gap about 10 seconds wide (not precisely).
During Delay BlackBox should not look "frozen", background tasks
must keep functioning, interface must be responsive.
Services.Action is not a valid solution, because the overall task is
to port some old code without refactoring. The solution using
Services.Action would require DoSomething1 and DoSomething2 to be
relocated to two different places.
Solution:
1. Enhance BlackBox background multitasking ability with patch
provided by Marco Ciot.
2. Implement message-processing loop in Delay (see below).
3. Use with caution. (I.e.: avoid unloading module while it is in
Delay, avoid recursive Delaying, some fixes required if Delay is to
be used from within Services.Actions, etc.)
PROCEDURE Delay* (delta: LONGINT); (* delta time to wait in [ms] *)
VAR
t: LONGINT; (* time to exit *)
res: INTEGER;
msg: USER32.Message;
quit: BOOLEAN;
BEGIN
t := Services.Ticks() + delta;
quit := FALSE;
REPEAT
res := USER32.GetMessageA(msg, 0, 0, 0);
IF res > 0 THEN
(* process messages *)
IF USER32.TranslateMDISysAccel(HostWindows.client, msg) = 0 THEN
res := USER32.TranslateMessage(msg);
(* warning! this module may be unloaded here: *)
res := USER32.DispatchMessageA(msg)
END;
(* update windows if message queue is empty *)
IF USER32.PeekMessageA(msg, 0, 0, 0, 0) = 0 THEN
Windows.dir.Update(NIL)
END;
(* process background tasks *)
Services.actionHook.Step;
ELSE
quit := TRUE;
IF res = 0 THEN (* WM_QUIT received, exit loop and repost *)
WinApi.PostQuitMessage(0)
ELSE (* some error occured; exit loop *)
END
END;
UNTIL quit OR (Services.Ticks() >= t);
END Delay;
Outcome: the provided Delay does the job, but there are some issues.
First of all - stability of resulting code. Second - shortcuts are
not working. (Probably there are other issues.) Overall: there is no
"legal" way to do this, but the hack is quite functional.
---=====---
Alexander
--- BlackBox
--- send subject HELP or UNSUBSCRIBE to blackbox{([at]})nowhere.xy
----boundary-LibPST-iamunique-682476207_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"
e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwIEhlbGxvLCBSb2JlcnQhXHBhcg0KXHBhcg0KQ1JE
U1VFPiBJIGFtIGFmcmFpZCB0aGF0IEkgZGlkIG5vdCBoYXZlIHRoZSB0aW1lXHBhcg0KQ1JEU1VF
PiB0byBmb2xsb3cgdGhlIGRldGFpbHMgb2YgdGhpcyBkaXNjdXNzaW9uLFxwYXINCkNSRFNVRT4g
YnV0IHRoZSB0b3BpYyBzZWVtZWQgdG8gYmUgb2YgcG90ZW50aWFsbHlccGFyDQpDUkRTVUU+IGdl
bmVyYWwgdXRpbGl0eS5ccGFyDQpccGFyDQpDUkRTVUU+IFdpdGggdGhlIGJlbmVmaXQgb2YgaGlu
ZHNpZ2h0LCBjYW4gc29tZW9uZSBwcm92aWRlXHBhcg0KQ1JEU1VFPiBhIHNpbXBsZSBleHBsYW5h
dGlvbiBvZiB0aGUgcHJ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IGxvb3AgYW5kIHJlcG9zdCAqKVxw
YXINCiAgICAgICAgICBXaW5BcGkuUG9zdFF1aXRNZXNzYWdlKDApXHBhcg0KICAgICAgICBFTFNF
ICAoKiBzb21lIGVycm9yIG9jY3VyZWQ7IGV4aXQgbG9vcCAqKVxwYXINCiAgICAgICAgRU5EXHBh
cg0KICAgICAgRU5EO1xwYXINCiAgICBVTlRJTCBxdWl0IE9SIChTZXJ2aWNlcy5UaWNrcygpID49
IHQpO1xwYXINCiAgRU5EIERlbGF5O1xwYXINClxwYXINClxwYXINCiAgT3V0Y29tZTogdGhlIHBy
b3ZpZGVkIERlbGF5IGRvZXMgdGhlIGpvYiwgYnV0IHRoZXJlIGFyZSBzb21lIGlzc3Vlcy5ccGFy
DQogIEZpcnN0IG9mIGFsbCAtIHN0YWJpbGl0eSBvZiByZXN1bHRpbmcgY29kZS4gU2Vjb25kIC0g
c2hvcnRjdXRzIGFyZVxwYXINCiAgbm90IHdvcmtpbmcuIChQcm9iYWJseSB0aGVyZSBhcmUgb3Ro
ZXIgaXNzdWVzLikgT3ZlcmFsbDogdGhlcmUgaXMgbm9ccGFyDQogICJsZWdhbCIgd2F5IHRvIGRv
IHRoaXMsIGJ1dCB0aGUgaGFjayBpcyBxdWl0ZSBmdW5jdGlvbmFsLlxwYXINClxwYXINCiAgXHBh
cg0KLS0tPT09PT0tLS1ccGFyDQogQWxleGFuZGVyXHBhcg0KXHBhcg0KLS0tIEJsYWNrQm94XHBh
cg0KLS0tIHNlbmQgc3ViamVjdCBIRUxQIG9yIFVOU1VCU0NSSUJFIHRvIGJsYWNrYm94QG9iZXJv
bi5jaFxwYXINCn19ADozNiA=
----boundary-LibPST-iamunique-682476207_-_---
Received on Tue May 23 2006 - 22:30:36 UTC
This archive was generated by hypermail 2.3.0
: Thu Sep 26 2013 - 06:27:58 UTC