Help with the responsive form

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
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

Help with the responsive form

Post by gutihz » Wed Sep 20, 2017 12:31 pm

Hi Everyone,
Need some help/understanding of the blog I read on this site here https://www.sapien.com/blog/2012/05/16/ ... ive-forms/

Now according to this blog, using the "Add-JobTracker" I can add a job. When I add my scriptblock, it works like a charm. However, I don't understand how I can add a foreach block that has 20 scriptblocks and pass that to the add-Jobtracker part and also update the progressbar with this.

I thought of calling the Add-jobtracker one by one but that would not only defeat the purpose of this blog but that would complicate the progressbar.

Anyone can explain how to accomplish this? Am I on the wrong path or didn't get the blog at all?
I added a modified downloaded project to what I'm asking and attached here.

Thanks in advance.

--------------------------------------
PowerShell Studio 2016 5.2.124
Version of product: 64
Operating system: Windows 7
OS: 64 bit
Attachments
Job Progress Bar.psf
(39.86 KiB) Downloaded 32 times

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

Re: Help with the responsive form

Post by jvierra » Wed Sep 20, 2017 12:50 pm

Why would you add 20 scripts to the same job? That makes no sense. Are you saying you want to create 20 separate jobs? Just add the jobs one at a time. JobTracker will manage multiple jobs.
If you have 20 steps and you want them to run parallel as one job then use a workflow and add that as a job.

User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

Re: Help with the responsive form

Post by gutihz » Wed Sep 20, 2017 3:28 pm

I have about 20 scripts that copies different things. Some are files as little as 10kb but some are as big as 10gb. What I'm trying to do is run everything at once without freezing the GUI but update the progress bar step as the small files complete.

I've been looking around, saw lots of examples but couldn't actually find one that explains what I'm trying to do.
Any ideas?

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

Re: Help with the responsive form

Post by jvierra » Wed Sep 20, 2017 3:48 pm

Just add 20 jobs to the JobTracker. They will all run in parallel.

User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

Re: Help with the responsive form

Post by gutihz » Wed Sep 20, 2017 4:04 pm

Hi jvierra,

Can you give me an example on how to add multiple jobs? I'm looking at the functions and don't see how that can be accomplished.

in a very simple example I tried doing :
foreach($i in $alljobs)
{
Add-jobtracker -scriptblock {$i}
}
which didn't work.

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

Re: Help with the responsive form

Post by jvierra » Wed Sep 20, 2017 4:13 pm

Just run Add-JobTracker multiple times. That is why it is called "Add"-JobTracker. The verb in PS always telss us what a CmdLet is trying to do.

User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

Re: Help with the responsive form

Post by gutihz » Wed Sep 20, 2017 4:23 pm

I get that. Thanks!
but that didn't update progressbar correctly. after the first script, it just completes as 100% as I mentioned in my initial post.

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

Re: Help with the responsive form

Post by jvierra » Wed Sep 20, 2017 4:28 pm

You will need to be sure the Update script runs for all jobs and that the progress steps are set correctly as well as the initial value. This will require using arithmetic and some serious design thinking to adapt the tracker to your requirements.

User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

Re: Help with the responsive form

Post by gutihz » Fri Sep 22, 2017 4:07 pm

So here is what I got so far...

I have a listview. So the folder gets copied over, but I can't update the subitem to either "Success" or "Error".

Any ideas what could be causing this issue?

Thanks in advance!

Code: Select all

$Test = $listview.Items.Add("Copying Folder")
				if (Test-Path "C:\Temp\From")
				{
					
					$Test.Subitems.Add("Folder exists")
					$Test.Subitems.Add("Copying...")
					Add-JobTracker -Name "Test" `
								   -JobScript $sb -ArgumentList "C:\Temp\From", 'C:\test' `
								   -CompletedScript {
						Param ($job)
						$Compare = CompareFolderSize "C:\Temp\From" 'C:\test'
							if ($Compare -eq $true)
							{
								$Test.Subitems[2].Text = "Success"
							}
							else
							{
								$Test.Subitems[2].Text = "Error"
							}
						
					}` -UpdateScript {
						Param ($job)
						while (Get-job -State "Running" | Where-Object{ $_.Name.Contains("Test") })
						{
							$Test.Subitems[2].Text = "Still Copying"
						}
	
					}
				}
				else
				{
					$copyError = "ERROR - Check folder or file"
					$Test.Subitems.Add($copyError)
				}

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

Re: Help with the responsive form

Post by jvierra » Fri Sep 22, 2017 4:32 pm

For this it would be easier to use a DataGridView,

In the update script you need to get a copy of the list item. The one you have is out of scope when the timer tick runs the "Update" block.

This should work although I would use a better name:

$global:Test =$listview.Items.Add("Copying Folder")

I would use:

$global:g_CurrentItem=$listview.Items.Add("Copying Folder")

Locked