Add-JobTracker - Job miss functions

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Add-JobTracker - Job miss functions

Post by ALIENQuake »

Hi,

I use Add-JobTracker for form timer in order to create responsibe UI. The background code is executed using:

Code: Select all

Add-JobTracker -JobScript {
		Set-Something (Get-Something -Reverse)
	} -CompletedScript {
		Param ($Job)
		$jobGUI = Receive-Job -Job $Job | Select-Object -Last 1
		if ($jobGUI -eq $true) {
			Set-Something (Get-Something)
			$Form1.ForeColor = 'Green'
			$Form1.Enabled = $true
			$Form1.Enabled = $true
		}
		if ($jobGUI -eq $false) {
			$Form1.ForeColor = 'Red'
		}
		$buttonStartJob.Enabled = $true
	} -UpdateScript {
		Param ($Job)
		$results = Receive-Job -Job $Job -Keep | Select-Object -Last 1
		
	} -Name "Test"
The problem is that background Job can't use functions like Set-Something (Get-Something) because it dosen't have it.

My workaround for this problem was:
- add "-InitializationScript" parameter to Add-JobTracker
- before execution, define Initializatin script as:

Code: Select all

$InitializationScript = [scriptblock]::Create(
@"
function Get-Something { ${function:Get-Something} }
function Set-Something { ${function:Set-Something } }
Set-Location $ScriptDirectory # to execute Job with the script or compiled exe directory
"@
)
Is there any better way for background Jobs which are created by JobTracker to have all functions available to use inside Job?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add-JobTracker - Job miss functions

Post by jvierra »

All functions must be included in the job script or placed In a file or module that can be loaded by the script.
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Re: Add-JobTracker - Job miss functions

Post by ALIENQuake »

I know, I just wonder if there is a better way that this @""@. Module might be ok but when you are dealing with compiled script to exe i won't help. Maybe Add-JobTracker should importa all variables and functions. Good idea for extension?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add-JobTracker - Job miss functions

Post by jvierra »

A compiled script can still load a module.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add-JobTracker - Job miss functions

Post by jvierra »

How to load functions froma scriptblock:

Code: Select all

$scriptfunctions = {
      function global:Test{ 'Hello World!' }
}
$scriptfunctions.Invoke()
Test
Now this can make functions available in the current script (EXE) and be easily passed to a job "InitializationScript".

The key is to add the "global" declaration to the function which allows it to be created in the global scope. The iit script does the "Invoke" under the covers so only the scrip block is required to be passed.
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Re: Add-JobTracker - Job miss functions

Post by ALIENQuake »

Yep, that is something which looks much better and doesn't feel like "hack". Thanks.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked