Pre-Start, Delayed Start and Post-Exit Tasks

03/10/2018 Alex Goodenough

Scripting Tasks

Any number of PowerShell, VBScript or other scripting technologies, executables, batch files etc can be run using these tags in the Programs.xml file. Anything started with <Start> and <Exit> tags in AppAcceleratorV.clc will run as a hidden process.

In AppAcceleratorV.clc all scripts and executables are executed asynchronously, a process is spawned to perform the task and execution proceeds immediately to the next start/exit tag.

Pre-Start Tasks

Tasks that need to be run BEFORE the application is started, can either be run with a <Start> entry in AppAcceleratorV.clc or an Always run condition in the Programs.xml.

AppAcceleratorV.clc - the addition of the line highlighted in bold below will start the custom-script.ps1 as a HIDDEN process, if you required the pre-start task not to be hidden, refer to Post-Exit Tasks below:

<Events>
    <Start>Cloudhouse.Container.Usage.exe /usageId:%UsageId% /event:RunStarted</Start>
    <Start>PowerShell .\custom-script.ps1</Start>
    <Exit>Cloudhouse.Container.Usage.exe /usageId:%UsageId% /event:RunStopped</Exit>	
</Events>

Programs.xml - a task can be set to execute before the application has started by adding an Always run condition. The below example will run the powershell task first and then the application:

<Program>
    <RunCondition>Always</RunCondition>
    <ProcessWindowStyle>Normal</ProcessWindowStyle>
    <Path>C:\powershell.exe</Path>
    <Args>C:\custom-script.ps1</Args>
    <WorkingDirectory />
    <WaitCondition TimeoutInSeconds="3">Exit</WaitCondition>
</Program>
<Program>
    <RunCondition>Initial</RunCondition>
    <ProcessWindowStyle>Normal</ProcessWindowStyle>
    <Path>%DefaultDir%\AppAcceleratorV.exe</Path>
    <Args>/f "%DefaultDir%\ProgData\Program Files (x86)\Notepad++\notepad++.exe"</Args>
    <WorkingDirectory />
    <WaitCondition TimeoutInSeconds="0">None</WaitCondition>
</Program>

Delayed Start Tasks

A second program can be started and it's startup delayed by using an Always <WaitCondition> and a TimeoutInSeconds value (t). This allows you to wait after starting a first program before starting the second.

<WaitCondition TimeoutInSeconds="t">Always</WaitCondition>

The below example will start notepad++, wait 5 seconds and then start notepad when Cloudhouse.Container.Run.exe /RunConditions Run1 is executed.

<Program>
<RunCondition>Run1</RunCondition>
<ProcessWindowStyle>Normal</ProcessWindowStyle>
<Path>%DefaultDir%\AppAcceleratorV.exe</Path>
<Args>/f "%DefaultDir%\ProgData\Program Files (x86)\Notepad++\notepad++.exe"</Args>
<WorkingDirectory />
<WaitCondition TimeoutInSeconds="5">Always</WaitCondition>
</Program>
<Program>
<RunCondition>Run1</RunCondition>
<ProcessWindowStyle>Normal</ProcessWindowStyle>
<Path>%DefaultDir%\AppAcceleratorV.exe</Path>
<Args>/f "%windir%\System32\notepad.exe"</Args>
<WorkingDirectory />
<WaitCondition TimeoutInSeconds="0">None</WaitCondition>
</Program>

Post-Exit Tasks

Tasks that need to be executed AFTER the application has exited, can either be run with an <Exit> entry in AppAcceleratorV.clc or an always condition in the Programs.xml.

AppAcceleratorV.clc - the addition of the line highlighted in bold below will start the custom-script.ps1 as a hidden process:

<Events>
    <Start>Cloudhouse.Container.Usage.exe /usageId:%UsageId% /event:RunStarted</Start>
    <Exit>Cloudhouse.Container.Usage.exe /usageId:%UsageId% /event:RunStopped</Exit>	
    <Exit>PowerShell .\custom-script.ps1</Exit>	
</Events>

Programs.xml - a task can be set to execute after the application has exited by adding an Exit wait condition on the application and an Always run condition BELOW your application run condition. The below example will run the powershell task after the application has exited:

<Program>
<RunCondition>Initial</RunCondition>
<ProcessWindowStyle>Normal</ProcessWindowStyle>
<Path>%DefaultDir%\AppAcceleratorV.exe</Path>
<Args>/f "%DefaultDir%\ProgData\Program Files (x86)\Notepad++\notepad++.exe"</Args>
<WorkingDirectory />
<WaitCondition TimeoutInSeconds="0">Exit</WaitCondition>
</Program>
<Program>
<RunCondition>Always</RunCondition>
<ProcessWindowStyle>Normal</ProcessWindowStyle>
<Path>%DefaultDir%\AppAcceleratorV.exe</Path>
<Args>/f "%windir%\System32\notepad.exe"</Args>
<WorkingDirectory />
<WaitCondition TimeoutInSeconds="8">None</WaitCondition>
</Program>

Changing the Order

The run conditions are executed top to bottom by default, this can be changed by using the Order attribute in the opening <Program> tag. The below example will run the PowerShell script and wait for it to exit before running Notepad++.

<Program Order="1">
<RunCondition>Run2</RunCondition>
<ProcessWindowStyle>Normal</ProcessWindowStyle>
<Path>%DefaultDir%\AppAcceleratorV.exe</Path>
<Args>/f "%DefaultDir%\ProgData\Program Files (x86)\Notepad++\notepad++.exe"</Args>
<WorkingDirectory />
<WaitCondition TimeoutInSeconds="0">None</WaitCondition>
</Program>
<Program Order="0">
<RunCondition>Run2</RunCondition>
<ProcessWindowStyle>Normal</ProcessWindowStyle>
<Path>C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe</Path>
<Args>C:\Users\alexg\Desktop\notepad++\custom-script.ps1</Args>
<WorkingDirectory />
<WaitCondition TimeoutInSeconds="3">Exit</WaitCondition>
</Program>

The TimeoutInSeconds attribute can be set alongside Exit in the <WaitConditions> tag. On the run condition with a timeout configured, this will display an error to the user if the process fails to close within the time specified.

In the example above, if the PowerShell process remains open for longer than 3 seconds then the user will see a "Failed to Launch application. Please contact support." error to notify them that something has gone wrong during startup. If the timeout is set to "0" it will wait indefinitely and never launch Notepad++ if the script never finishes executing and closes powershell.exe.

Was this article helpful?

Table of Contents

    Can't find what you're looking for?

    Contact Support