Progressbarproblems after Change to new Powershell Studio

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 8 years and 5 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
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Progressbarproblems after Change to new Powershell Studio

Post by monoeagle »

Hi@All,
After the last Editor Session was killed with an mysql error I have to type all again. :evil: :evil: :evil:

Powershell Studio: 2014 --> working
Powershell Studio: 2015(4.2.95) --> not working

System: Win7 x64

Used PS Version: v2 or v4 x64, on both the progressbar doesn't run on the Powershell Studio 2015

Problem:
On the 2014 Powershell Studio my Progressbar was working.
After fresh install ob 2015 Powershell Studio the Progressbar is not working.


Application:
I have a DataGridView with some Columns with Buttons. If the Button was pressed the Progressbar should show the % Action.

Part where my Datagrid reacts:

Code: Select all

$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
	
#....		
	if ($_.ColumnIndex -eq 5)
	{
		$timer.Start()
		$pgb_robocopy.Value = 0
		$global:currentcnt = 0
						
		# get file count	
		$global:totalcnt = (Robocopy "C:\Quelle" "C:\Ziel" /MIR /S /ETA /L /NC /NP).length - 1
				
		$i = 0
		
		# create job
		$global:job = Start-Job -ScriptBlock {
			robocopy "C:\Quelle" "C:\Ziel" /MIR /S /ETA /NC /NP /R:10 /W:10 /TEE /LOG:"C:\temp\logfile_$(get-date -f MM-dd-yyyy).log"
		}
			
		#...		
	}
	
#.....
}



# 
$handler_tick = {
	$global:currentcnt += ($global:job | Receive-Job).Count
	$percent = ($global:currentcnt / $global:totalcnt) * 100
	
	if ($percent -lt 100)
	{
		$pgb_robocopy.Value = $percent
	}
	else
	{
		$pgb_robocopy.Value = 100
		$timer.Enabled = $false
		$global:job | remove-job -ErrorAction SilentlyContinue
	}
}




Definition Variables in Global.ps1

Code: Select all

$global:job = $null
$global:currentcnt = 0
$global:totalcnt = 0

Startup.ps1

Code: Select all

function Main {
	Param ([String]$Commandline)

	#Timer settings
	$timer.add_tick($handler_tick)
	$timer.Enabled = $false
	$timer.Interval = 500
	
	
	if((Call-MainForm_psf) -eq "OK")
	{
		
	}
	
	$timer.dispose()
}


If I use this Example standalone it works with both Powershell Studio Version:

Code: Select all

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$progress = New-Object System.Windows.Forms.ProgressBar
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$timer = New-Object System.Windows.Forms.Timer
#endregion Generated Form Objects


$global:job = $null
$global:currentcnt = 0
$global:totalcnt = 0

$handler_button1_Click= {
    if ($button1.Text -eq "Start"){
        $timer.Start()
        $button1.Text = "Running"
        $button1.Enabled = $false
        $progress.Value = 0

        $global:currentcnt = 0
        # Anzahl der Dateien ermitteln
        $global:totalcnt = (robocopy "C:\quelle" "C:\ziel" /S /ZB /ETA /L /NS /NC /NJH /NJS /NDL ).length -1

        $i = 0
        $global:job = Start-Job -ScriptBlock {
            robocopy "C:\quelle" "C:\ziel" /S /ZB /ETA /NS /NC /NJH /NJS /NDL /NP /R:10 /W:10
        }
    }else{
        $timer.Stop()
        $global:job | stop-job
        $global:job | remove-job -Force
        $button1.Text = "Start"
    }

}
$handler_tick = {
    $global:currentcnt += ($global:job | Receive-Job).Count
    $percent = ($global:currentcnt / $global:totalcnt) * 100

    if ($percent -lt 100) {
        $progress.Value = $percent
    }else{
        $progress.Value = 100
        $timer.Enabled = $false
        $button1.Enabled = $true
        $button1.Text = "Start"
       $global:job | remove-job -ErrorAction SilentlyContinue
    }
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 74
$System_Drawing_Size.Width = 292
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.MaximizeBox = $False
$form1.Name = "form1"
$form1.Text = "ProgressForm"


$button1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 41
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 268
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 1
$button1.Text = "Start"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($handler_button1_Click)

$form1.Controls.Add($button1)

$progress.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$progress.Location = $System_Drawing_Point
$progress.Name = "progress"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 268
$progress.Size = $System_Drawing_Size
$progress.TabIndex = 0

$form1.Controls.Add($progress)

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)

#Timer settings
$timer.add_tick($handler_tick)
$timer.Enabled = $false
$timer.Interval = 500

#----- Show Form
$form1.ShowDialog() | out-null

# WICHTIG Timer resourcen freigeben 
$timer.dispose()
Do you have any Idea or hints why my parts doesn't work on Powershell Studio 2015, but on 2014? Or have I done somethink extremly wrong by mirgrate the example in my source?

regards
monoeagle
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Progressbarproblems after Change to new Powershell Studio

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, monoeagle.

Did you remember to include the following?
  • 1. Product, version and build (e.g. Product: PowerShell Studio 2014, Version & Build: 4.1.71. Version and build information can be found in the product's About box accessed by clicking the blue icon with the 'i' in the upper right hand corner of the ribbon.)
    2. Specify if you are running a 32 or 64 bit version
    3. Specify your operating system and if it is 32 or 64 bit.
    4. Attach a screenshot if your issue can be seen on the screen
    5. Attach a zip file if you have multiple files (crash reports, log entries, etc.) related to your issue.
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Progressbarproblems after Change to new Powershell Studi

Post by davidc »

You probably have a scoping issue with the timer.

PowerShell V2 handled scoping differently than V3 and up. Add the timer in the designer of the form instead of creating it in the startup file.

PowerShell Studio also has a Job Tracker framework in the Control Sets that will handle jobs and the timer for you.

Refer to the following blog article for more details on the job tracker:

https://www.sapien.com/blog/2012/05/16/powershell-studio-creating-responsive-forms/

Out of curiosity, where did you find that example? Is that something you created or found on the web?

David
David
SAPIEN Technologies, Inc.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Progressbarproblems after Change to new Powershell Studi

Post by monoeagle »

Thanks for the Tip with the Job Tracker Framework.
I'll will view at it.

The Example was the support from the user: colinardo

[url]**w.administrator.de/frage/powershell-progressbar-will-285535.html#comment-1046478[/url]

He is very fast if someone have a powershell question/problem.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Progressbarproblems after Change to new Powershell Studi

Post by monoeagle »

The Job-Tracker works. But why starts the progressbar running at the end? If the amount of data isn't too much it's ok, but if I copy a lot of files the progressbar runs at the end. :?:

Code: Select all


		$pgb_robocopy.Value = 0
		
		#Create a New Job using the Job Tracker
		Add-JobTracker -Name "JobName" `
					   -JobScript {
			#--------------------------------------------------
			#TODO: Set a script block
			#Important: Do not access form controls from this script block.
			
			Param ($Argument1)#Pass any arguments using the ArgumentList parameter
			
			robocopy "C:\Program Files" "C:\Ziel" /MIR /S /ETA /NC /NP /R:10 /W:10 /TEE /LOG:"C:\temp\logfile_$(get-date -f MM-dd-yyyy).log"
			
			#for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
			
			#--------------------------------------------------
		}`
					   -UpdateScript {
			Param ($Job)
			$results = Receive-Job -Job $Job | Select-Object -Last 1
			
			if ($results -is [int])
			{
				$pgb_robocopy.Value = $results
			}
		}`
					   -CompletedScript {
			Param ($Job)
			$pgb_robocopy.Value = 100
		}
		

Or is the process-Tracker the right choice?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Progressbarproblems after Change to new Powershell Studi

Post by davidc »

In this script you changed the robocopy command and are using a different Receive-Job command. That is why it is not working as expected.

David
David
SAPIEN Technologies, Inc.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Progressbarproblems after Change to new Powershell Studi

Post by monoeagle »

davidc wrote:In this script you changed the robocopy command and are using a different Receive-Job command. That is why it is not working as expected.

David

I changed the Robocopy Command to have a greater amount of Data to copy. The DIR c:\quelle is just 3MB. "program files" are 650MB.

In your example I just changed your Progressbar "progressbar1" to my "$pgb_robocopy" and adding my robocopy command to the -JobScript part. The "for" loop could be removed because its just an example or not?

Where is the mistake?

Why should I have a different Receive-Job if I set the correct value?

I just copy your example and fill in my progressbarname and the action(robocopy).

greeting
monoeagle
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Progressbarproblems after Change to new Powershell Studi

Post by monoeagle »

Do you have any idea/hint how the progressbar will work with the robocopy action?

:?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Progressbarproblems after Change to new Powershell Studi

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUI FORUM BY MODERATOR]


You change the Receive-Job line from:
PowerShell Code
Double-click the code block to select all.
($global:job | Receive-Job).Count
to
PowerShell Code
Double-click the code block to select all.
Receive-Job -Job $Job | Select-Object -Last 1
Which does not work in this case. Be careful when you copy paste or use existing examples. They may only work in specific instances.

Also verify that your robocopy command still produces the same output. I noticed you are saving to log now.


David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Progressbarproblems after Change to new Powershell Studi

Post by jvierra »

By default robocopy has no output until it is completed. You can choose to have the log output to the console which will produce output or you can have it output a tick for each file moved.
This topic is 8 years and 5 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