async update GUI text

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.
Locked
sctsprin
Posts: 4
Joined: Tue Oct 02, 2018 8:17 pm

async update GUI text

Post by sctsprin » Sun Nov 04, 2018 6:55 pm

Hi, I've borrowed this powershell studio form code from the forum to run a background task then display the result in a textbox,
However the textbox flashes the result, I would like the result to remain until the next updated value is displayed,
Could someone please help?

Windows 10, Powershell studio 2018, v5.5.155

thanks
james
Attachments
updateGUI.psf
(21.44 KiB) Downloaded 12 times

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

Re: async update GUI text

Post by jvierra » Sun Nov 04, 2018 7:08 pm

Change this line:
$textbox1.Text = $results

To this:
$textbox1.Text += $results

sctsprin
Posts: 4
Joined: Tue Oct 02, 2018 8:17 pm

Re: async update GUI text

Post by sctsprin » Sun Nov 04, 2018 10:39 pm

Thanks for the reply, but that ends up just adding to the last result, so i end up with "12345...." etc
I want to only see the current text result displayed until it's replaced by the new result, so "1" is displayed until it's replaced by "2" etc.

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

Re: async update GUI text

Post by jvierra » Sun Nov 04, 2018 10:46 pm

If the updates happen too fast then it will always look funny.

What is the purpose of just outputting numbers as fast as you can?

Code: Select all

		Param ($Job)
		if($results = Receive-Job -Job $Job | Select-Object -Last 1){
		    $textbox1.Text = $results
        }

sctsprin
Posts: 4
Joined: Tue Oct 02, 2018 8:17 pm

Re: async update GUI text

Post by sctsprin » Mon Nov 05, 2018 3:12 am

I actually just want to refresh once a minute, but need the last result to display until the next update, which currently isn’t happening, instead it’s flashing for a split second and is then empty u til the next flash update

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

Re: async update GUI text

Post by jvierra » Mon Nov 05, 2018 3:27 am

The code I posted does exactly that you just need to set the timer to the value you want.

sctsprin
Posts: 4
Joined: Tue Oct 02, 2018 8:17 pm

Re: async update GUI text

Post by sctsprin » Mon Nov 05, 2018 6:34 pm

thanks, i found my mistake, i removed the if statement surrounding the textbox1.text = $results, which caused empty data to be displayed

thanks
james

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

Re: async update GUI text

Post by jvierra » Mon Nov 05, 2018 6:52 pm

That is not what is happening. The "if statement prevents a null value from being assigned to the text which was the initial issue with your code. Displaying once a second comes when the sleep is set to 60 seconds and not 1000 milliseconds.

Locked