I think Write-Progress is a rather beautiful Cmdlet. In fact Sharepoint makes use of it with its Start-SPAdminJob commandlet.
All fine and dandy, the problem is that Start-SPAdminJob does not correctly "dispose" of the Write-Progress dialog. It is never set to 100 percent complete which means it just stays in the Powershell dialog until you exit the script - this in turn hides part of the messages underneath the "progress window".
Is there any way I can force an existing Write-Progress to "exit" or be set to 100% complete? Any way how I could find out the ID of the progress the Start-SPAdminJob cmdlet is using - that way I could manually set the percentage.
You could stop the progress bar appearing in the first place by doing the following beforehand:
$ProgressPreference = "SilentlyContinue";
You could then restore the preference to "Continue" afterwards. Not much help if you actually want the bar of course...
This code forces progress bar to be set to 100% complete and hides it:
Write-Progress "Done" "Done" -completed
Related
I'm tring to prevent users to rapidly spam-click a button, which would freeze the app possibly for minutes while the code is being executed many times.
$searchBtn_clicked = {
$searchBtn.Enabled = $false
... some code that fills a listview from search result
$searchBtn.Enabled = $true
}
My problem is: the code above will be executed as many times as the button is clicked, no matter what. Disabling the button after a click changes nothing. I also tried adding a start-sleep 2 before Enabling it back.
First click triggers the code, subsequent clicks are onto the disabled button... and will still trigger the Click event, as soon as the button becomes Enabled again.
What is happening here? Some kind of asynchronous magic? All click events are somehow queued and only being processed after the button is Enabled again?
I'm new to PowerShell and very confused.
Add [System.Windows.Forms.Application]::DoEvents():
$searchBtn.Add_Click({
$this.Enabled = $false
# ... some code that fills a listview from search result
# flush out all pending events
[System.Windows.Forms.Application]::DoEvents()
$this.Enabled = $true
})
Hope that helps
Windows messages are queued and processed in order. If your code is busy, these messages (spam-clicks) are stored in that queue and processed when possible. DoEvents() tells the window to pocess all messages currently in the message queue, effectively flushing it.
I have a script which filters an excel sheet (schedule) and displays the result through Out-Gridview. When the user clicks "Cancel" it opens the excel sheet after closing the Out-Gridview. When the user clicks "Ok" it only closes the Out-Gridview. But these actions are not visible for the user. So can i rename the "Ok" and "Cancel" button of Out-Gridview to lets say "Close Schedule" and "Open Excelsheet"?
And also, is it possible to run the rest of the script without waiting for the response, but keep the buttons working?
I know that if i don't use an output mode the rest of the script runs without waiting, but with outputmode (which gives me buttons) it waits for the response.
I use this code to filter a schedule and it would be nice if i can use the rest of the program without shutting down the rest of the program, maybe by starting another instance of powershell or something?
$Schedule = $ResultsTable | Out-Gridview -OutputMode Single -Title "Schedule"
if ($Schedule -eq $Null) {
Start-Process "$Fileserver\Logistics_share\Planning_Alg\TS Delivery schedule rev2.0.xlsx"
}
If it can be done with only a visual overlay, it is ok by me (Buttons)
I have a script which uses asynchronous runspaces in order to display a XAML/WPF GUI while processing stuff in the background. When I click the button, it crashes, but only when in a specific state. Let me elaborate.
The script begins with pinging a long list of servers to ensure that they're up. After that, it runs through a few SQL queries, WMI calls, other logic, processes all of the data, and then updates my DataGrid in the other window one row at a time with the information.
The button simply brings up a WinForm confirmation:
#$Global:uihash is my synchronized hashtable.
$Global:uihash.Button.add_click({if([System.Windows.Forms.MessageBox]::Show `
("Yes or No?", "Question",[System.Windows.Forms.MessageBoxButtons]::OKCancel) -eq "OK")
{Do stuff back in main PS Window}
If I press the button while the script is pinging all of the devices, it works perfectly. If I click the button at any other time during the script, the PS Windows immediately flip to not responding and I have to kill them.
From the application event log:
The program PowerShell_ISE.exe version 10.0.10586.117 stopped
interacting with Windows and was closed.
I'm like 95% sure it's because pinging devices is the only part of the script which has absolutely zero interaction with the other window, I just don't know how to fix this, or even work around it.
I'm wondering if anybody has seen or experienced this or something similar before, and how you got around it. Any help would be greatly appreciated. Thanks!
I was never able to completely figure this out, but I did find a good workaround for me. I put the main chunk of my script in a Do-Until loop, and made the button click change a global variable I created. That broke it out of the loop, closed the window, and let all the finishing tasks run without crashing.
#$Global:uihash is my synchronized hashtable.
#Create synchronized hash table variable
$Global:uiHash.Add(“End”,$false)
#Create Click Event
$Global:uihash.Button.add_click({if([System.Windows.Forms.MessageBox]::Show
("Yes or No?", "Question",[System.Windows.Forms.MessageBoxButtons]::OKCancel) -eq "OK")
{$Global:uiHash.End = $true}
Do {A whole bunch of stuff}
Until ($global:uiHash.end -eq $true)
#Close the window
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.Window.close() },"Normal")
#Then process the rest of the script
I have a powershell script with a simple form which is working. It runs an external application (USMT scanstate / loadstate) and the functionality is all fine.
However when it's running the user has no feedback that it is working, so initial users were thinking something had gone wrong.
I've added a marquee progress bar to run at the bottom of the form to indicate it is working.
I can use the:
$progressbar1.visible = $False
Property to show whether it is visible or not, but I'm not sure how to make this dynamic. If I set to false then call the form with .ShowDialog() then try to change it later to True nothing actually changes in the form.
Am I missing something? Is there a way to dynamically change the visibility of a form object?
EDIT :: Adding the .Visible property change to the .add_click of the button.
$buttonYes.add_click({$progressbar1.visible = $True;Action $type;$confirm.Close()})
This does get the progress bar to appear, however the Marquee is not running which is strange, so it just shows an blank box...
EDIT 2 :: OK so looks like it does work, but only when run from the ISE. I've tried it on another system and running from command line or shortcut doesn't work. I've tried setting STA but still no go, the progress bar shows but now Marquee effect.
could someone please tell me how to disable cancel button in a job's progress entry in Progress View tab in eclipse rcp application. i have not been able to locate any references on the web aside from the ones that suggest the use of ProgressMonitorDialog. using the dialog, however, is not an option, as the Progress View must remain in a form of a view.
i have come upon ProgressMonitorPart, which sounds like something that i can use. if that is the case, how do i go about passing it to Job.run(IProgressMonitor)?
thank you for your time!
You don't. You can call Job#setSystem() on your Job so it's hinted as not to be shown, but you don't get to spin off jobs that the user can't at least ask you to cancel. The stop button does little more than set the progress monitor as having been canceled--it's still up to your running Job to check the progress monitor and behave itself. Or not.
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fruntime_jobs_progress.htm