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
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with the responsive form

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

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

Re: Help with the responsive form

Post by gutihz » Sat Sep 23, 2017 3:07 am

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?

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

Re: Help with the responsive form

Post by jvierra » Sat Sep 23, 2017 3:27 am

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

Re: Help with the responsive form

Post by gutihz » Sat Sep 23, 2017 7:13 am

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?

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

Re: Help with the responsive form

Post by jvierra » Sat Sep 23, 2017 10:49 am

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

Re: Help with the responsive form

Post by gutihz » Sat Sep 23, 2017 12:33 pm

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 34 times

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

Re: Help with the responsive form

Post by jvierra » Sat Sep 23, 2017 12:59 pm

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.

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

Re: Help with the responsive form

Post by jvierra » Sat Sep 23, 2017 1:45 pm

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"
		}
	}
}

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

Re: Help with the responsive form

Post by jvierra » Sat Sep 23, 2017 2:58 pm

I took another quick look. You have no timer event. You must have deleted it somehow.

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

Re: Help with the responsive form

Post by jvierra » Sat Sep 23, 2017 5:33 pm

See the following for a complete working example: http://tech-comments.blogspot.com/2017/ ... -form.html

Locked