{"id":2118,"date":"2013-11-07T20:14:47","date_gmt":"2013-11-07T08:14:47","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=2118"},"modified":"2013-11-07T21:07:51","modified_gmt":"2013-11-07T09:07:51","slug":"creation-stories","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/2118\/","title":{"rendered":"Creation Stories"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>In the beginning there was <del>The Word<\/del> a HWND.  But not all gifts of creation can be relied upon, as I just learned.  Or rather, remembered.<\/p>\n<p><!--more--><\/p>\n<p>As regular readers will know, I am currently polishing up my <strong>Smoketest<\/strong> framework for release (don&#8217;t worry, this post won&#8217;t take long and then I can get back to that job).<\/p>\n<p>I made a recent change that I was particularly pleased with.  It was a trivial thing, but one of those small things that makes a big difference.<\/p>\n<p>Previously the GUI console would indicate the currently executing test with a particular icon, replaced once the test was complete with another icon indicating the test result.  This was fine, but on longer running tests was&#8230; well.. a bit boring.  So I animated it.<\/p>\n<p>Drawing on an experience from many years ago and combining that with message based thread synchronization, I opted to implement a motile thread <sup>*1<\/sup>  which would provide the beating heart of my animation.  This thread would simply pump out messages at the rate of 12 per second, incrementing a frame counter.<\/p>\n<p><sup>*1<\/sup> &#8211; <em>my own term &#8211; and a specific class in my threading library &#8211; for a lightweight, fire-and-forget thread that just keeps churning away on it&#8217;s allocated, usually trivial task indefinitely.  When I devised the class I purloined the name from a superb science fiction novel (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Commonwealth_Saga\">Pandora&#8217;s Star<\/a>) that I was reading at the time.<\/em><\/p>\n<p>Upon receiving the message my console UI would update the icons of any currently running test(s) to the image for the frame indicated in the message.<\/p>\n<p>And it all worked beautifully:<\/p>\n<figure id=\"attachment_2119\" aria-describedby=\"caption-attachment-2119\" style=\"width: 320px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Very-Pretty.gif?ssl=1\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Very-Pretty.gif?resize=320%2C240&#038;ssl=1\" alt=\"Animation working well\" width=\"320\" height=\"240\" class=\"size-full wp-image-2119\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-2119\" class=\"wp-caption-text\">Animation working well<\/figcaption><\/figure>\n<p>Things didn&#8217;t go so well when I rebuilt the test project (in this particular case for my Bonjour components &#8211; are you reading this Stefan ? :)) for Delphi 7.<\/p>\n<figure id=\"attachment_2120\" aria-describedby=\"caption-attachment-2120\" style=\"width: 320px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Not-so-hot.gif?ssl=1\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Not-so-hot.gif?resize=320%2C240&#038;ssl=1\" alt=\"Oh.  Dear.  Oh.  Dear.\" width=\"320\" height=\"240\" class=\"size-full wp-image-2120\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-2120\" class=\"wp-caption-text\">Oh.  Dear.  Oh.  Dear.<\/figcaption><\/figure>\n<p>This problem has been bugging me for a couple of weeks now but I had a bit of a gap in my schedule to spend some time looking into it, and fortunately the problem and the solutions turned out to be very simple.<\/p>\n<p>I was creating my animation beat thread in the FormCreate event of my main form and passing the form window handle at that point.<\/p>\n<p>In Delphi 2010 and later this was fine.  In Delphi 7 (and 2006 and possibly other versions though I didn&#8217;t check) the thread would happily run and pump out the messages as it should, but the<strong> Application.ProcessMessage()<\/strong> method would never receive them.<\/p>\n<p>No doubt a fair few people out there are jumping up and down and pointing and shouting <strong>I KNOW WHAT&#8217;S WRONG I KNOW WHAT&#8217;S WRONG<\/strong>.  And fortunately it didn&#8217;t take me long to arrive at the answer myself.<\/p>\n<p>I remembered that over the years and various <strong>Delphi<\/strong> versions the order in which events occur during form creation has changed on different occasions.  Also, the various conditions that result in window handles being created and subsequently re-created has also changed.<\/p>\n<p>The reason that my animation messages were not being received was that the window handle to which they were being sent no longer existed because my console form window (the underlying HWND) had been destroyed and re-created at some point after providing the <strong>Handle<\/strong> to the thread in the <strong>FormCreate()<\/strong> event.<\/p>\n<p>But only in these <em>earlier<\/em> versions of <strong>Delphi<\/strong>.<\/p>\n<p>Having realised the problem, the solution was very simple: Move the code which assigned the <strong>HWND<\/strong> to the animation thread to an override of the form&#8217;s <strong>CreateHandle<\/strong> method.  Then no matter if, when or how many times my form&#8217;s <strong>HWND<\/strong> was re-created, the animation thread would always be sure to be posting it&#8217;s messages to the right window:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  procedure TSmoketestConsole.CreateHandle;\r\n  begin\r\n    inherited;\r\n    FPS.Handle := self.Handle;\r\n  end;\r\n<\/pre>\n<p>And hey presto, my oh-so-pretty little animation works in all versions of Delphi from the venerable 7 onward.<\/p>\n<figure id=\"attachment_2121\" aria-describedby=\"caption-attachment-2121\" style=\"width: 320px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Thats-Better.gif?ssl=1\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Thats-Better.gif?resize=320%2C240&#038;ssl=1\" alt=\"All is once more right with the world.  Phew.\" width=\"320\" height=\"240\" class=\"size-full wp-image-2121\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-2121\" class=\"wp-caption-text\">All is once more right with the world.  Phew.<\/figcaption><\/figure>\n<p>Apologies for the <strong>Screen-o-matic<\/strong> water-mark on these files by the way. I&#8217;ll be taking the time to look for a (Mac) tool for capturing such video vignettes but for now the free version of this did the job, and the watermark is the price I had to pay.  Hey ho.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> In the beginning there was The Word a HWND. But not all gifts of creation can be relied upon, as I just learned. Or rather, remembered.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[292],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-ya","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1330,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1330\/","url_meta":{"origin":2118,"position":0},"title":"Of Threads, Stacks and RAM &#8211; Part 2","date":"29 Nov 2012","format":false,"excerpt":"In the previous post in this series, we saw that the number of threads that a given process could support was determined by a number of factors, of which the stack size reserved for each thread was key. We also saw how we could change the stack size used by\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":375,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/375\/","url_meta":{"origin":2118,"position":1},"title":"Delphi 2009 &#8211; StringPerformance Redux","date":"22 Sep 2008","format":false,"excerpt":"It looks like I may have jumped the gun with my conclusions from the previous exercise to benchmark string performance in Delphi 2009.\u00a0 Following a useful exchange in the comments with Kryvich I corrected a small discrepancy in the tests and made some changes to the performance testing subsystem within\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/delphi2009-stringperformance-chart.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1930,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1930\/","url_meta":{"origin":2118,"position":2},"title":"VCL Threading &#8211; Indeterminate Lifetimes","date":"18 Oct 2013","format":false,"excerpt":"Sometimes when you launch a thread you don't know when it will complete whatever processing it is tasked with. Sometimes you do. Sometimes it may never complete and will require that you expressly terminate it. Usually any given thread will have a lifecycle that is at least consistently one or\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1297,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1297\/","url_meta":{"origin":2118,"position":3},"title":"Of Threads, Stacks and RAM &#8211; Part 1","date":"28 Nov 2012","format":false,"excerpt":"Roberto Schneiders recently drew my attention to the first post on his new blog (which I can recommend as a good read :) ), presenting the results of some performance testing of DataSnap that he had been involved with which proved to be very interesting (if initially somewhat disappointing). But\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2012-11-28-at-20.24.21-.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1925,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1925\/","url_meta":{"origin":2118,"position":4},"title":"VCL Threading &#8211; Synchronization","date":"16 Oct 2013","format":false,"excerpt":"Although I am using Oxygene a lot these days, Delphi remains my tool of choice for Win32 (and x64) development, together with the VCL. Hence this post. A long time ago, in a galaxy far far away, Delphi was a Windows only development tool. 16 was the number of the\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3037,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/3037\/","url_meta":{"origin":2118,"position":5},"title":"ReRaise Exception Bug in 10.4.1 x86 Compiler","date":"09 Dec 2020","format":false,"excerpt":"I just ran into a very frustrating issue in Delphi 10.4.1 with long-standing, basic functionality that is now broken in certain circumstances. Fortunately there is a work-around in those certain circumstances, but it's not pretty. The Problem: Re-Raising an exception causes an immediate Access Violation error at the raise statement\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/image.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2118"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=2118"}],"version-history":[{"count":4,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2118\/revisions"}],"predecessor-version":[{"id":2125,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2118\/revisions\/2125"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=2118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=2118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=2118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}