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
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 »

If you are running multiple jobs then you will need to do it like this:

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) {
			$job.ListViewItem.Subitems[2].Text = "Success"
		} else {
			$job.ListViewItem.Subitems[2].Text = "Error"
		}
		
	}` -UpdateScript {
		Param ($job)
		while (Get-job -State "Running" | Where-Object{ $_.Name.Contains("Test") }) {
			$job.ListViewItem.Subitems[2].Text = "Still Copying"
		}
		
	}
	$job = Get-Job Test
	$job | Add-Member -NoteProperty ListViewItem -Value $test
} else {
	$copyError = "ERROR - Check folder or file"
	$Test.Subitems.Add($copyError)
}
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

Thanks for the replies and suggestions. Unfortunately neither of them worked.
I added

Code: Select all

[void][System.Windows.Forms.MessageBox]::Show("update part")


and also in the CompletedScript part and nothing shows up. I wonder if there is something wrong in the function?
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 haven't implemented the whole JobTracker. It requires some global collections and a timer event. It is nit just a single function. No time tick - no results.
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

Obviously I'm new to this. I read the blog again, looked at the examples given and couldn't figure it out.

As I mentioned I do have multiple jobs that I want to copy from and to. I have a foreach loop that I pass my scriptblock with my arguments. Basically the same code in my previous post. Do I make the listview or datagrid as you suggested as a global variable?
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 »

The ListView is already a variable in scope. All controls are always in-scope. What is in your timer code? Have you started the timer? Is this a PSF or is it a hand coded form?
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Help with the responsive form

Post by gutihz »

I didn't think I had to start the timer since it is triggered inside the "Add-JobTracker" function.

Code: Select all

#Start the Timer
		if (-not $timerJobTracker.Enabled)
		{
			$timerJobTracker.Start()
		}
mine is a psf. I attached the file.
Attachments
Copy.psf
(97.56 KiB) Downloaded 128 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 »

Use a datagrid.
Use a CSV to create load the DGV. Include the folder path in the CSV.

You loop can get all values from the CSV/DGV.

Before doing that create a new form and add Tracker. Test the code suggestions with one job until you understand how it works. Once you understand then move the lesson to your form an manage jobs in a loop.

Unfortunately you have chosen a set of design requirements that are well beyond your current skill set. You will have to slowly work through this. Build one small piece at a time until you understand how it works. This is the nature of all programming. No programmer comes with built-in knowledge of any technology. Programming is about teaching yourself how things 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 »

Food for thought. Here is how to approach managing jobs. Onc you understand how this works you can experiment with changing controls or adding other complexities.

# define this at script level so we don't create hundreds of them
$fso = New-Object -ComObject Scripting.FileSystemObject

Code: Select all

# define this at script level so we don't create hundreds of them
$fso = New-Object -ComObject Scripting.FileSystemObject

$jobScript = {
	Param ($strSource,$strDest)
	Robocopy.exe $strSource $strDest /MIR /V /R:2 /W:5
	
}

$updateScript = {
	Param ($job)
	$job.DataGridRow.Cells['Status'].Value = $job.State
}

$completedScript = {
	Param ($job)
	
	if ($fso.GetFolder($path1).Size -eq $fso.GetFolder($path1).Size) {
		$job.DataGridRow.Cells['Status'].Value = "Success"
	} else {
		$job.DataGridRow.Cells['Status'].Value = "Error"
	}
}

$MainForm_Load={
	$copyJobs = Import-Csv CopyJobs.Csv
	$datagridview1.DataSource = [ArrayList]$copyJobs
}

$buttonRunJobs_Click= {
	foreach ($row in $datagridview1.SelectedRows){
		$Name = $row.Cells['Name'].Value
		if (Test-Path $row.Cells['SourcePath'].Value){
			
			$jobParams = @{
				Name = $row.Cells['Name'].Value
				JobScript = $sb
				UpdateScript = $updateScript
				CompletedScript = $completedScript
				ArgumentList = @($row.Cells['SourcePath'].Value, $row.Cells['TargetPath'].Value)
			}
			$job = Add-JobTracker @jobParams

			$job = Get-Job $row.Cells['Name'].Value
			$job | Add-Member -NoteProperty DataGridRow -Value $row
		}else{
			$row.Cells['Status'].Value = "ERROR - Check folder or file"
		}
	}
}
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 »

I took another quick look. You have no timer event. You must have deleted it somehow.
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 »

See the following for a complete working example: http://tech-comments.blogspot.com/2017/ ... -form.html
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