Function based form updates not executing sequentially

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
User avatar
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

Function based form updates not executing sequentially

Post by Technosapien » Thu Jul 30, 2015 6:14 pm

I have a form that takes a hostname as input then;

1) When user clicks check status, a function is called to check if the pc exists in AD.
2) If it does it grabs info from AD and updates relevant textboxes on form.
3) Tries to ping device using .Net object.
4) if successful displays IP, if fails displays "Ping Failed".

For some reason the textbox updates from step 2 will not execute until the results from the ping is received and ready to update it's textbox.

Why? These are separate functions.

I've tried to paste the code here but won't let me as new account and thinks it includes off-site URLs.

Check it here at PasteBin: (pastebin . com / tE0iFT5f) Remove spaces.

EDIT: Adding info requested from automated post.

Did you remember to include the following?
Product: PowerShell Studio 2015 4.2.89 64 bit
Operating system: Windows 7 Enterprise 64 bit.

How it should work:
1) User enters hostname then clicks check status.
2) Host found in AD then retrieves info and updates AD status, OU and bitlocker Key textboxes and the AD group listbox.
3) Calls ping function, updates IP Address field to "Pinging" then ping returns result to IP textbox.

What is happening:
1) User enters hostname then clicks check status.
2) Host found in AD then retrieves info but AD status, OU and bitlocker Key textboxes don't update and only the first AD group found is returned to listbox.
3) Calls ping function, doesn't update IP Address field to "Pinging".
4) App/Form freezes until failed ping returns result to IP textbox then all other textboxes and listbox updates simultaneously.

I have screenies and can provide the code but the forum is preventing me from posting either stating "Antispam: You can't have off-site URLs in your posts until you have posted a few times".

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

Function based form updates not executing sequentially

Post by SAPIEN Support Forums » Thu Jul 30, 2015 6:14 pm

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

Thank you for posting, Technosapien.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
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
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

Re: Function based form updates not executing sequentially

Post by Technosapien » Sat Aug 01, 2015 7:37 pm

Bump

Is this the right forum or is there a different one for license holders :?:

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

Re: Function based form updates not executing sequentially

Post by jvierra » Sat Aug 01, 2015 8:19 pm

What is your question. You describe a lot of scenarios but fail to ask a question.

Are you saying that your code won't run?

User avatar
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

Re: Function based form updates not executing sequentially

Post by Technosapien » Sun Aug 02, 2015 12:50 am

jvierra wrote:What is your question. You describe a lot of scenarios but fail to ask a question.

Are you saying that your code won't run?
My code does run. My problem is I have functions that include textbox updates. These updates included as part of my functions are not executing until all the functions finish.

I believe this is due to the way PowerShell Studio executes it's textbox updates.

I'm asking is this behaviour a result of how PowerShell Studio updates textboxes to try and do them all at once and if so is there a workaround for it?

Otherwise, not a product but script question, is there any obvious bug in my linked code causing this?

Thanks for your response :)

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

Re: Function based form updates not executing sequentially

Post by jvierra » Sun Aug 02, 2015 2:41 am

I have not really looked at your code because it I not in a PFF file so it is difficult to reproduce you issue.

Windows in an event driven system. A forms cannot update controls inside of an event because the code block all other code from executing.

One what to force an update is to call "DoEvents" after updating a control.

Attached is a demo.

Also see blog posts on how to design forms and how to make forms responsive.
Attachments
Demo-ControlUpdate.psf
Demo allowing controls to update.
(22.52 KiB) Downloaded 97 times

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

Re: Function based form updates not executing sequentially

Post by jvierra » Sun Aug 02, 2015 3:04 am

You can also just use Refresh on most controls although this will not let all forms events get handled and events in the message loop can block the refresh. "DoEvents" works under more circumstances.

Here is how to use Refresh:
PowerShell Code
Double-click the code block to select all.
function WriteToTextbox {
	param (
		[switch]$withevents
	)
	$this.Enabled = $false
	$textbox1.Clear()
	$textbox1.Text = 'Start'
	1..10 |
	%{
		$textbox1.Lines += "$_ New line"
		if ($withevents) {
			$textbox1.Refresh()
		}
		sleep 1
	}
	$textbox1.Lines += 'Finish'
	$this.Enabled = $true
}

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

Re: Function based form updates not executing sequentially

Post by jvierra » Sun Aug 02, 2015 3:18 am

To directly answer your concerns:
Technosapien wrote:
I believe this is due to the way PowerShell Studio executes it's textbox updates.

I'm asking is this behaviour a result of how PowerShell Studio updates textboxes to try and do them all at once and if so is there a workaround for it?
Tis has nothing to do with PowerShell Studio. The code is running in PowerShell. Run it as a PS1 file called from PowerShell and it will behave exactly the same way.
Technosapien wrote:Otherwise, not a product but script question, is there any obvious bug in my linked code causing this?
Yes - it is a design and Windows understanding "bug". The code was not designed to work cooperatively with Windows. This is a common mistake with those new to scripting and scripting or programming Windows Forms. It is the event driven nature of a modern computer that you need to be aware of. There are many technical reasons for, and caveats to this. It is enough to know that, in PowerShell forms, events block execution until the event code exits.

User avatar
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

Re: Function based form updates not executing sequentially

Post by Technosapien » Sun Aug 02, 2015 4:35 am

Thanks JVierra :)

I'll review the code you've provided and read up more on DoEvents

Locked