{"id":15343,"date":"2018-07-09T06:00:36","date_gmt":"2018-07-09T13:00:36","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=15343"},"modified":"2018-07-06T13:08:37","modified_gmt":"2018-07-06T20:08:37","slug":"pause-and-continue-added-to-powershell-service-packager-engine","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2018\/07\/09\/pause-and-continue-added-to-powershell-service-packager-engine\/","title":{"rendered":"PAUSE and CONTINUE added to PowerShell Service Packager Engine"},"content":{"rendered":"<p>We recently added support for a more refined handling of events in PowerShell based Windows services\u2014allowing for the handling of <em>Start<\/em>, <em>Stop<\/em>, and <em>Run<\/em> events separately.\u00a0 Please see this article for details.\u00a0<a title=\"https:\/\/www.sapien.com\/blog\/2018\/02\/27\/new-powershell-service-template-for-primalscript-2018\/\" href=\"https:\/\/www.sapien.com\/blog\/2018\/02\/27\/new-powershell-service-template-for-primalscript-2018\/\">https:\/\/www.sapien.com\/blog\/2018\/02\/27\/new-powershell-service-template-for-primalscript-2018\/<\/a><\/p>\n<p>The latest service builds of PrimalScript 2018 and PowerShell Studio 2018 include the ability to handle <em>Pause<\/em> and <em>Continue<\/em> events.<\/p>\n<p>The PowerShell Service Template has been updated to contain functions for these new operations:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/image.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/image_thumb.png\" alt=\"image\" width=\"1368\" height=\"446\" border=\"0\" \/><\/a><\/p>\n<p>The new template functions merely set a global flag to indicate that the service is paused. The main loop of the service needs to be adjusted to handle this flag accordingly.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/image-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/image_thumb-1.png\" alt=\"image\" width=\"1419\" height=\"702\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>If you transform an existing service to support <em>Pause<\/em> and <em>Continue<\/em>, you will need to incorporate this mechanism similar to what is shown in the screenshot above.<\/p>\n<p>You may ask why <em>Pause<\/em> does not simply suspend the thread the PowerShell engine is running on, and <em>Continue<\/em> would resume the thread. It would certainly add <em>Pause<\/em> and <em>Continue<\/em> to the service without you having to do anything.<br \/>\nThe answer is because it is never a good idea to suspend a thread or a process from the outside. You simply never know what the current state of it is. Imagine that your service just sent a query to a server for a large amount of WMI data. Right after sending the query, your service\u2019s thread gets paused\u2014or frozen. When the user decides to continue the service, your code will find that the query\u2019s result is no longer there\u2014it is even likely that the server you connected to is no longer online. It would be very difficult to make every single line in your code \u2018interruptible\u2019 so it will not crash or cause an exception when this happens.<\/p>\n<p>It is much better to leave the handling of <em>Pause<\/em> and <em>Continue<\/em> to your code, so you can decide when is the appropriate time to suspend doing whatever your service is doing. This will also allow your service to handle a <em>Stop<\/em> request properly while in a paused state.<\/p>\n<h2>Feedback<\/h2>\n<p>As always, if you have any ideas, comments, or feedback, please head over to our forum and post in the appropriate section: <a title=\"https:\/\/www.sapien.com\/forums\/index.php\" href=\"https:\/\/www.sapien.com\/forums\/index.php\">https:\/\/www.sapien.com\/forums\/index.php<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We recently added support for a more refined handling of events in PowerShell based Windows services\u2014allowing for the handling of Start, Stop, and Run events separately.\u00a0 Please see this article for details.\u00a0https:\/\/www.sapien.com\/blog\/2018\/02\/27\/new-powershell-service-template-for-primalscript-2018\/ The latest service builds of PrimalScript 2018 and PowerShell Studio 2018 include the ability to handle Pause and Continue events. The PowerShell Service [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1143,703,410,624,21,25],"tags":[1258,1257,28,1016,37,300],"class_list":["post-15343","post","type-post","status-publish","format-standard","hentry","category-powershell","category-powershell-studio","category-primalscript-software-news","category-service-build","category-software-news","category-windows-powershell","tag-continue","tag-pause","tag-powershell","tag-powershell-studio","tag-primalscript","tag-service"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=15343"}],"version-history":[{"count":10,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15343\/revisions"}],"predecessor-version":[{"id":15355,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15343\/revisions\/15355"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=15343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=15343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=15343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}