Using Job Tracker

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
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Using Job Tracker

Post by jsira2003@yahoo.com »

I use the latest version of Powershell Studio 2018. I need to understand how to use the job tracker to avoid a not responding error. I read your earlier articles regarding handling loops and forms. Assuming my job is function test, how would I use job tracker to control all aspects of the job. There is no loop in function test and I don't have a progress bar to update. It is just a long slow process that could take many hours and prevent the form from handling events. I would like to see how I make the call from the form for function test.

I assume that even though a new process will be started to call my function, my calling program will wait until the job completes before continuing. This is my understanding of how the job tracker works.

I already loaded the job tracker into my form. Please summaries how I make the call. My function has 3 or more arguments and returns an array and a variable.

thank you for your help,

John Sirabella
cody m

Re: Using Job Tracker

Post by cody m »

[TOPIC MOVED BY MODERATOR]
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Using Job Tracker

Post by davidc »

I recommend looking at the "Button - Start Job" control set (ToolBox->Control Sets->Button - Start Job:
Button - Start Job Control Set.png
Button - Start Job Control Set.png (26.48 KiB) Viewed 4226 times
The control set provides an example of how to start a job and update the button when the job completes.

As with all jobs, you will have to include the function definition in the Job Script. When job completes, you will have to use Receive-Job cmdlet to get the results of the job. Note: Receive-Job will return whatever is in the pipeline upon completion.
  1. $results = Receive-Job -Job $Job
If it will take hours, you might want to prevent the user from closing the form or run the script in a separate process.
David
SAPIEN Technologies, Inc.
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com »

I want to run this job so i don't get a not responding. However I do not want the script to continue. I would like for the job to complete, get the results and then the script in the form continues on the next line. Am I understanding this correct?

thanks,
John
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com »

My apologies for not being clear. I have a button that calls a function. That button calls a 2nd function. The 2nd function is the one that causes the not responding message. I was thinking that in the first function I would create a job that would call the 2nd function. The 1st function would have to wait until the 2nd function completes.

My goal is to avoid a not responding message between these 2 function calls. I am wondering if this is possible? The program works correctly its just because of the long delay I receive the not responding message. The Doevents worked nicely in the loop.


thanks,
John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using Job Tracker

Post by jvierra »

Without an example of your code it is not possible to understand how to advise you.

I think once you have experimented with the JobTracker an understand how it works you will be able to see how to use it with your code.

For tasks running more than many minutes without producing output you should consider using a PowerShell scheduled job. Scheduled jobs continue even after the PowerShell session terminates. They can also continue across reboots.

about_Scheduled_Jobs
about_Scheduled_Jobs_Advanced
about_Scheduled_Jobs_Basics


If you need a very long running job then also consider a workflow which also will run across reboots and power failures.


about_Checkpoint-Workflow
about_Suspend-Workflow
about_WorkflowCommonParameters
about_Workflows
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com »

I came up with the following to eliminate the (Not responding) on the top of the form as my form execute a function. It works!!! I'd appreciate your critic of what I created. I did not include the baselineSB scriptblock. The baseline function can take anywhere from minutes to 20 hours depending on the dataset.

$myjob = Start-Job -ScriptBlock { Baseline $args[0] $args[1] $args[2] $args[3] $args[4] } -InitializationScript $baselineSB -ArgumentList $server.server, $sharePath.sharepath, $sharePath.FileMask, $sharePath.recurse, $serverLogBaseline
while (!(Wait-Job $myjob))
{

[System.Windows.Forms.Application]::DoEvents()
}
$completeFileList = Receive-Job $myjob
$fileCount = $completeFileList.length
Get-Job | remove-job

Thank you,
John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using Job Tracker

Post by jvierra »

That won't work because Wait-Job is blocking and will block until the job completes.
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com »

I thought I was good. I know it failed now. This is the egg I have to crack!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using Job Tracker

Post by jvierra »

Don't use Wait-Job. Use a timer of use JobTracker.
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