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.
This topic is 6 years and 6 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
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Help with the responsive form

Post by gutihz »

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 138 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with the responsive form

Post by jvierra »

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
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with the responsive form

Post by jvierra »

Just add 20 jobs to the JobTracker. They will all run in parallel.
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with the responsive form

Post by jvierra »

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
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with the responsive form

Post by jvierra »

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
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

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)
				}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with the responsive form

Post by jvierra »

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")
This topic is 6 years and 6 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