Unable to maximize UI during processing function

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 10 years and 4 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
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Unable to maximize UI during processing function

Post by owinsloe »

okay...I have googled until out of breath and I understand all the ins-n-outs with powershell not being a real graphical engine. I have a script (quite complex) that runs a number of processes via System.Diagnostics.ProcessStartInfo asynchronously. I have a progress bar that I also update to indicate the progress. I have the code working perfectly...and quite happy UNTIL the ui is minimized. There are two undesirable characteristics when this occurs;

1. When you mouse over over the TaskBar thumb nail, the progress bar is not updated to reflect what has been done (it's stuck at the level that it was at when the window was minimized).

2. The ui can not be maximized. Clicking on the thumbnail or right-click maximize simply results in a bell and the ui remains minimized. I'm aware that what is occurring is a pseudo async threading and user response will not return until the button click foreach function has completed.

I have tried messing with timer events to do windows event type calls and the best I could achieve was to set a timer event to manually raise the window with a windowstate.normal call, update the progressbar then minimize again. This achieved the thumbnail progressbar update but had a ugly flashing appearance when the window maximized and still did not provide the ability for the user to manually raise the window by mouse-clicking on the taskbar thumbnail.

I suspect the only solution I have is to DISABLE the minimize & restore fields on the "_ [ ] [x]" area. If anyone has a better suggestion, would really appreciate.

I have provided a stripped down example of code that mimics the issue, but I'm not sure that anything can be done to resolve or work-around the problem.

#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: d664be25-84fc-43a1-ad78-0239908e2a7f
# Source File: C:\scripts\Timer\MainForm.pff
#------------------------------------------------------------------------
#========================================================================
# Code Generated By: SAPIEN Technologies, Inc., PrimalForms 2011 v2.0.21
# Generated On: 1/11/2013 3:17 p.m.
# Generated By: Owen Winsloe
# Organization: Jade Software
#========================================================================
#----------------------------------------------
#region Application Functions
#----------------------------------------------

#endregion Application Functions

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-MainForm_pff {

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
#endregion Import Assemblies

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$MainForm = New-Object 'System.Windows.Forms.Form'
$progressbar1 = New-Object 'System.Windows.Forms.ProgressBar'
$button1 = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------

#$MainForm_Load={
# #TODO: Place custom script here
# $timer = New-Object System.Timers.Timer
# $timer.Interval = 10000
# $timer.AutoReset = $false
#}

$button1_Click={
#TODO: Place custom script here
foreach ( $i in 1..100 ){
start-sleep 1
$progressbar1.value=$i
[System.Windows.Forms.Application]::DoEvents()
$mainform.refresh()
}
}

$MainForm_Load={
#TODO: Place custom script here

}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

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

$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$button1.remove_Click($button1_Click)
$MainForm.remove_Load($MainForm_Load)
$MainForm.remove_Load($Form_StateCorrection_Load)
$MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
#
# MainForm
#
$MainForm.Controls.Add($progressbar1)
$MainForm.Controls.Add($button1)
$MainForm.ClientSize = '532, 204'
#region Binary Data
$MainForm.Icon = [System.Convert]::FromBase64String('AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAA
AAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A
////AP///wD///8A////AP///wD///8A////AP///wD///8A/f39A/H8/R/7/f0J////AP///wD/
//8A////AP///wD///8A////AP///wD///8A////AP///wD3/P0PsO/4kHPf9Olf1/P/7Pn8Ov//
/wD///8A////AP///wD///8A////AP///wD///8A////AP///wDq+vwpct/14kzK8P9Jxe//SLju
/+j1/Dr///8A////AP///wD///8A////AP///wD///8A////AP///wD0/P0Vd+D14EjG8P9Douj/
PGvS/0Z/wf/n7/Y6////AP///wD///8A////AP///wD///8A////AP///wD///8A3PT6PEq87/8/
m+b/RH7E/4qsdv+xpmL/9fTrOv///wD///8A////AP///wD///8A////AP///wD///8A////ALa3
8WE4SN7/QG/P/5GpbP/DwZ7z4Ozpefv8/BH///8A/f39APL29Tj///8A////AP///wD///8A////
AP///wCGhOigN0TZ/1qbrv+/rXT/7vTzRv///wD///8A////ANrp5qW31M/9+Pr6Jv///wD///8A
////AP///wD///8AbW3ixjZF1/90p4z/w7+b7v39/QD///8A////AP///wDT4+CktdPN/+zz8Fz/
//8A////AP///wD///8A////AHd35LU3Rdb/ZqOV/8C5jvz8/fwR////AP///wD///8A0OHdwrbR
y//w9PNH////AP///wD///8A////AP///wCamuqIN0TW/0uIvP+wolz/0+Lcr/z9/Ar9/f0A6fHv
Zby2i/3DwqH4/f39Cf///wD///8A////AP///wD///8A3Nz2MmV83+NUvbj/arqR/7mkXf/Cw6H3
xcuy8sGsc//Ljyr/1J1G+vv27Sn///8A////AP///wD///8A////AP///wD5/PwMgd3U0F7Irv9j
wZz/ebB6/7eVO//Kjyz/05xF+vPkzFf///8A////AP///wD///8A////AP///wD///8A////APf8
/BWi6OKtX9LA/VnLtP/DwIzU69Cnlvr17ST///8A////AP///wD///8A////AP///wD///8A////
AP///wD///8A////APH7+h/L8u5W8fv5Hf///wD///8A////AP///wD///8A////AP///wD///8A
////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD/
//8A//8AAP//AAD4/wAA8P8AAOD/AADg/wAA4f8AAMPPAADDzwAAw88AAMHPAADgDwAA8B8AAPg/
AAD//wAA//8AAA==')
#endregion
$MainForm.Name = "MainForm"
$MainForm.StartPosition = 'CenterScreen'
$MainForm.Text = " Jade Software Corporation"
$MainForm.add_Load($MainForm_Load)
#
# progressbar1
#
$progressbar1.Location = '16, 22'
$progressbar1.Name = "progressbar1"
$progressbar1.Size = '490, 29'
$progressbar1.TabIndex = 1
#
# button1
#
$button1.Location = '174, 143'
$button1.Name = "button1"
$button1.Size = '187, 52'
$button1.TabIndex = 0
$button1.Text = "GO"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_Click)
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $MainForm.WindowState
#Init the OnLoad event to correct the initial state of the form
$MainForm.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$MainForm.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $MainForm.ShowDialog()

} #End Function

#Call the form
Call-MainForm_pff | Out-Null
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to maximize UI during processing function

Post by jvierra »

When that happens it is because you are in an event and not taking any more events.

I don't think anyone wants to manually go through 100 or so lines of code. If you upload the PFF file I will take a look at it to see where you are blocking the message loop.

The UI will not work correctly if you are sitting in a loop in an event or if you are calling a long running process in an event. "DoEvents" is only minimally helpful when in a tight loop.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to maximize UI during processing function

Post by jvierra »

Try it this way:|
PowerShell Code
Double-click the code block to select all.
$button1_Click={
    	#TODO: Place custom script here
        foreach ( $i in 1..100 ){
        	start-sleep -Milliseconds 200
        	$progressbar1.value=$i
        	[System.Windows.Forms.Application]::DoEvents()
			$progressbar1.Refresh()
	    }
	}
Run powershell with -mta switch. It is required for the type of form you are using.
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: Unable to maximize UI during processing function

Post by owinsloe »

Thanks for that...I actually found the issue. I had STA checked in my IDE. I checked this as I was using clipboard COM objects but it seems to have a nasty side-effect. So I'll leave unchecked and try & work-around any issues that may arise with the clipboard functions.

The other issue that you may be able to assist with is where all this started. I would like the progressbar to advance on the thumb nail when the ui has been minimized to the taskbar. It must be an eventing issue but I have so far been unable to locate. If you could steer me in the right direction that would be brilliant. Thanks (pff attached)
Attachments
MainForm.pff
(197.02 KiB) Downloaded 230 times
This topic is 10 years and 4 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