{"id":18749,"date":"2022-11-01T20:48:47","date_gmt":"2022-11-01T15:18:47","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/"},"modified":"2022-11-01T20:48:47","modified_gmt":"2022-11-01T15:18:47","slug":"solved-close-the-form-on-certain-hours-of-the-day","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/","title":{"rendered":"[Solved] Close the form on certain hours of the day"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-25800052\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"25800052\" data-parentid=\"25798341\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I suggest you investigate Waitable timers.  These can be set to set to fire after a specific period of time (like a regular TTimer) or at a specified time of day, which is exactly what you need in this case.<\/p>\n<p>In your form create\/show event, create a waitable timer and set it to the required time that you wish it to &#8216;fire&#8217; (it will be only one of your candidate close times, i.e the next one to occur after the current time).  In your case I believe you mentioned the countdown starts 90 seconds before the close time, so this is your &#8220;due time&#8221; for the waitable timer (next T &#8211; 90 secs).<\/p>\n<p>The due time you set must be specified in <strong>FILETIME<\/strong> and must be in UTC, not local time.  This is fiddly, but not especially difficult.<\/p>\n<p>Calculate the next auto close time, less 90 seconds.  Then use <strong>DateTimeToSystemTime(localTDateTime, localSYSTEMTIME)<\/strong> to the resulting <strong>TDateTime<\/strong> value in a <strong>SYSTEMTIME<\/strong> representation which you can then pass to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-nz\/library\/windows\/desktop\/ms725485(v=vs.85).aspx\">TzSpecificLocalTimeToSystemTime()<\/a> to convert to a UTC <strong>SYSTEMTIME<\/strong>.<\/p>\n<p>From there you simply then convert your UTC <strong>SYSTEMTIME<\/strong> to <strong>FILETIME<\/strong> (<strong>SystemTimeToFileTime()<\/strong> in <strong>SysUtils<\/strong>). <\/p>\n<p>The callback proc is a first class proc, not a form method, and must conform to the expected callback signature.<\/p>\n<p>The callback proc will be called in a separate thread so your callback implementation to start the countdown timer must be thread safe.  The simplest way to achieve this is to exploit message queues and simply send (or post) a message to the form which in turn responds by starting the countdown timer.  To ensure the right window handle is used, this can be passed to the callback proc.  Since a HWND fits in a pointer you can just pass the HWND in the pointer directly, by typecasting.<\/p>\n<p>Your callback proc will look something like this:<\/p>\n<pre><code>procedure TimerCallbackProc(aData: Pointer; aTimerLo, aTimerHi: DWORD);\nbegin\n  PostMessage(HWND(aData), MM_STARTCOUNTDOWNTIMER, 0, 0);\nend;\n<\/code><\/pre>\n<p>Where <strong>MM_STARTCOUNTDOWNTIMER<\/strong> is a private, <strong>WM_USER<\/strong> based message that the form handles to start the countdown timer:<\/p>\n<p><strong>NOTE:<\/strong>  Your form must cancel the callback timer when it is closed, either before the timer has &#8216;fired&#8217; or as a result of it.<\/p>\n<p>Putting all of that together, you should end up with something like:<\/p>\n<pre><code>const\n  MM_STARTCOUNTDOWNTIMER = WM_USER + 1;\n\n\ntype\n  TMyForm = class(TForm)\n    fCloseCountdownTimer: TTimer;\n    fCloseTimer: HANDLE;\n    ..\n    procedure MMStartCountdownTimer(var aMessage: TMessage); message MM_STARTCOUNTDOWNTIMER;\n  end;\n\n\n  procedure TMyForm.FormCreate(Sender: TObject); \n  begin\n     ..\n\n     ..\n     fCloseTimer := CreateWaitableTimer( .. );\n     SetWaitableTimer( fCloseTimer, dueTime, 0, TimerCallbackproc, Pointer(Handle), TRUE );\n  end;\n\n\n  procedure TMyForm.FormClose(Sender: TObject); \n  begin\n    CancelWaitableTimer( fCloseTimer );\n  end;\n\n\n  procedure TMyForm.MMStartCountdownTimer(var aMessage: TMessage); \n  begin\n    fCloseCountdownTimer.Enabled := TRUE;\n  end;\n<\/code><\/pre>\n<p>NOTE: The final <strong>TRUE<\/strong> parameter in the call to <strong>SetWaitableTimer()<\/strong> in the code above ensures that if the system is suspended at time that the timer fires, then the system will wake in order to process the timer.  If this is not what you want, then simply pass FALSE, and the timer will not wake a sleeping system (but your form will not now close automatically if the due time has been and gone while the system was asleep).<\/p>\n<p>For further and more specific details, I suggest you refer to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-nz\/library\/windows\/desktop\/ms687012(v=vs.85).aspx\">Waitable Timer API documentation from Microsoft<\/a><\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Close the form on certain hours of the day <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I suggest you investigate Waitable timers. These can be set to set to fire after a specific period of time (like a regular TTimer) or at a specified time of day, which is exactly what you need in this case. In your form create\/show event, create a waitable timer and set it to the &#8230; <a title=\"[Solved] Close the form on certain hours of the day\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\" aria-label=\"More on [Solved] Close the form on certain hours of the day\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[740],"class_list":["post-18749","post","type-post","status-publish","format-standard","hentry","category-solved","tag-delphi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Close the form on certain hours of the day - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Close the form on certain hours of the day - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I suggest you investigate Waitable timers. These can be set to set to fire after a specific period of time (like a regular TTimer) or at a specified time of day, which is exactly what you need in this case. In your form create\/show event, create a waitable timer and set it to the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T15:18:47+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Close the form on certain hours of the day\",\"datePublished\":\"2022-11-01T15:18:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\"},\"wordCount\":477,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"delphi\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\",\"name\":\"[Solved] Close the form on certain hours of the day - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T15:18:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Close the form on certain hours of the day\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Close the form on certain hours of the day - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Close the form on certain hours of the day - JassWeb","og_description":"[ad_1] I suggest you investigate Waitable timers. These can be set to set to fire after a specific period of time (like a regular TTimer) or at a specified time of day, which is exactly what you need in this case. In your form create\/show event, create a waitable timer and set it to the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T15:18:47+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Close the form on certain hours of the day","datePublished":"2022-11-01T15:18:47+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/"},"wordCount":477,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["delphi"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/","url":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/","name":"[Solved] Close the form on certain hours of the day - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T15:18:47+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-close-the-form-on-certain-hours-of-the-day\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Close the form on certain hours of the day"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18749"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18749\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}