Asked by:
Powershell Gui freeze during WMI query execution

Question
-
Hi all,
So i have this PS Gui created where i have couple of buttons.
When i press one of the buttons i want the form to close and to proceed with the execution. However what i see is that the form freezes - i cannot close or move it, and on the background the code is still running. In the end the form closes after the code is completed.
Here is what i do:
$buttonFinish_Click = { if ($radio.Checked -eq $true) { Write-Host "starting" $form.Close() $wmiObject =Get-WmiObject -Namespace root\cimv2 -Class Win32_Product }
As you can imagine the WMI query takes long time to get through all products in Win32_Product class and until it completes the form stays open even though the close instruction is prior to the wmi query.
I am seeking help here, how can i close the form and then run the wmi?
I was looking into jobs and/or DoEvents(), but couldn't get it to work either way..
- Moved by Bill_Stewart Wednesday, May 30, 2018 6:49 PM Abandoned
Wednesday, March 28, 2018 1:03 PM
All replies
-
Why close the form?
Yes. A form is frozen during the execution of the event. To prevent this use a job.
\_(ツ)_/
Wednesday, March 28, 2018 1:16 PM -
I close the form, because i do not need it anymore. My code might run 1 hour and i don't want for the form to show frozen 1 hour..
I did try using jobs, but got the same freeze.
Wednesday, March 28, 2018 1:18 PM -
The form cannot be closed before starting the job. You cannot wait on the job. Give the job a name so you can find it after the form closes.
$buttonFinish_Click = { if ($radio.Checked){ Write-Host starting $temp = Start-Job -Name products -ScriptBlock {Get-WmiObject Win32_Product} $form.Close() } }
\_(ツ)_/
- Edited by jrv Wednesday, March 28, 2018 1:24 PM
Wednesday, March 28, 2018 1:23 PM -
Yep, that worked..
The first time i didn't assign "Receive-Job" to any variable.
Now it is and i can work with the job solution :)
Wednesday, March 28, 2018 7:06 PM