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.
User avatar
jsira2003@yahoo.com
Posts: 40
Joined: Fri Apr 07, 2017 9:47 am

Using Job Tracker

Post by jsira2003@yahoo.com » Tue May 15, 2018 8:18 am

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

User avatar
cody m
Site Admin
Posts: 123
Joined: Thu Jun 01, 2017 7:20 am

Re: Using Job Tracker

Post by cody m » Tue May 15, 2018 8:25 am

[TOPIC MOVED BY MODERATOR]
Cody McCants
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5329
Joined: Thu Aug 18, 2011 4:56 am

Re: Using Job Tracker

Post by davidc » Tue May 15, 2018 8:58 am

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 512 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: 40
Joined: Fri Apr 07, 2017 9:47 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com » Tue May 15, 2018 10:54 am

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: 40
Joined: Fri Apr 07, 2017 9:47 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com » Tue May 15, 2018 11:15 am

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

User avatar
jvierra
Posts: 12538
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using Job Tracker

Post by jvierra » Tue May 15, 2018 1:44 pm

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: 40
Joined: Fri Apr 07, 2017 9:47 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com » Wed May 16, 2018 6:12 pm

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

User avatar
jvierra
Posts: 12538
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using Job Tracker

Post by jvierra » Wed May 16, 2018 6:32 pm

That won't work because Wait-Job is blocking and will block until the job completes.

User avatar
jsira2003@yahoo.com
Posts: 40
Joined: Fri Apr 07, 2017 9:47 am

Re: Using Job Tracker

Post by jsira2003@yahoo.com » Wed May 16, 2018 7:13 pm

I thought I was good. I know it failed now. This is the egg I have to crack!

User avatar
jvierra
Posts: 12538
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using Job Tracker

Post by jvierra » Wed May 16, 2018 7:37 pm

Don't use Wait-Job. Use a timer of use JobTracker.

Locked