UI freeze during script running.

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 5 years and 7 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
Mr.Wang
Posts: 1
Last visit: Wed Aug 08, 2018 3:27 am

UI freeze during script running.

Post by Mr.Wang »

Code: Select all

$autodeploy_Load={
	UpdateNavButtons
}
function UpdateNavButtons
{
	if (Test-Path C:\update.csv)
	{
		$a = Get-Content "C:\update.csv"
		$a = $a -split ","
		$user.Text = $a[1]
		$pwd.Text = $a[3]
		$client.Text = $a[5]
		$back_db.Text = $a[7]
		$back_client.Text = $a[9]
	}
}

$button1_Click={
	#TODO: Place custom script here
	$csv = New-Object -TypeName System.Collections.ArrayList
	$csv.Add("account,$($user.Text)") | Out-Null
	$csv.Add("password,$($pwd.Text)") | Out-Null
	$csv.Add("path,$($client.Text)") | Out-Null
	$csv.Add("backup_path,$($back_db.Text)") | Out-Null
	$csv.Add("backup_path2,$($back_db.Text)") | Out-Null
	$csv | Out-File C:\update.csv -Force | Out-String
	$RichTextBox1.Select()
	$RichTextBox1.Select($RichTextBox1.TextLength,0)
	$RichTextBox1.ScrollToCaret()
		Invoke-Dploy_ps1
	$richtextbox1.Text |Out-File C:\PACKPRESS\logs\Update\info\info.log -encoding utf8 -Force -Append
	#Invoke-Dploy_ps1 -txt $richtextbox1
}
Here are some sample codes above, in the bold part 'Invoke-Dploy_ps1', it will appendtext to $richtextbox many times.
My problem is UI will freeze until the whole script completed.
Is there any solution to update the UI during the script running ?

If you can help me ,Thank you very much
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: UI freeze during script running.

Post by jvierra »

Place the following in your loop.

[System.Windows.Forms.Application]::DoEvents()
This topic is 5 years and 7 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