PAUSE and CONTINUE added to PowerShell Service Packager Engine

We recently added support for a more refined handling of events in PowerShell based Windows services—allowing for the handling of Start, Stop, and Run events separately.  Please see this article for details. https://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 Template has been updated to contain functions for these new operations:

image

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.

image

 

If you transform an existing service to support Pause and Continue, you will need to incorporate this mechanism similar to what is shown in the screenshot above.

You may ask why Pause does not simply suspend the thread the PowerShell engine is running on, and Continue would resume the thread. It would certainly add Pause and Continue to the service without you having to do anything.
The 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’s thread gets paused—or frozen. When the user decides to continue the service, your code will find that the query’s result is no longer there—it 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 ‘interruptible’ so it will not crash or cause an exception when this happens.

It is much better to leave the handling of Pause and Continue 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 Stop request properly while in a paused state.

Feedback

As always, if you have any ideas, comments, or feedback, please head over to our forum and post in the appropriate section: https://www.sapien.com/forums/index.php