GUI project going grey, (Not Responding)

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.
User avatar
Posts: 2
Joined: Mon Feb 01, 2016 5:11 pm

GUI project going grey, (Not Responding)

Post by o0beaner » Sun Feb 07, 2016 4:47 pm

Hey there!

I am working on my first GUI project with Powershell Studio 2015, and have run into an odd issue.

Let me preface this with saying, I have read the articles about creating responsive forms and have used that information extensively on my project.

Most of the functions that I have created in this script are stored in the globals.ps1 file, and the events on my forms call those functions.

I can run the entire application without fail from the IDE (i.e., clicking Run/Debug), but when I export the application to an executable, it fails at a very particular point. Essentially the form works in 3 high-level steps, each fired by a button control:

1) Query and select a target against which work will be performed
2) Collect credentials, authenticate against and open session to host
3) Do work

The first two steps work pretty well with good consistency, but the button to fire step 3 will cause the application to fail every single time.

I have tried adjusting the tasks that are actually carried out by the button (in the project, called $main_button_run) to more and more menial tasks in order to test and debug, but it fails regardless of what I configure it to do. Is there something i am missing here?

Currently the code that is fired by a button click is as follows:

Code: Select all

Write-Debug 'run clicked'
$main_label_elapsed_static.Visible = $false
$main_label_elapsed.Visible = $false
The function being called is stored in globals.ps1, and does the following:

Code: Select all

function LogRun-Setup
# Checks for presence of security group, creates/attaches if necessary.
# A PS session is opened for the LogRun task, the root working path is
# set and the start time is stored. A folder is created at the root
# working path.

Write-Debug 'Logrun-Setup init'
$main_label_status.text = "Creating security group..."
Write-Debug 'CheckSG'
$main_label_status.text = "Opening session..."
$global:jobList = @()
$global:startDTM = (Get-Date)
$global:RootPath = "c:\WorkSpacesLogs\"
$main_label_status.text = "Creating working directory..."
Write-Debug 'adding rootpath job'
Add-RemoteJobTracker -Name "CreateRootPath" -JobScript `
$global:RootPath = $args[0]
Remove-Item $global:RootPath -recurse -Force -ErrorAction SilentlyContinue | out-null
New-Item $global:RootPath -type directory -force | out-null
} -ArgumentList $global:RootPath -CompletedScript `
Write-Debug "RootpathCreated"
Wait-Job -Name "CreateRootPath"
Write-Debug "LogRun-Setup Complete"
$main_label_status.text = "Gathering Workspace Logs..."
The Add-RemoteJobTracker is a just a version of SAPIEN's Add-JobTracker that has been modified to use invoke-command in order to execute remote tasks, as well as to close the PSSessions when they are no longerneeded.

Is there something stupid here that I am not seeing?
(37.52 KiB) Downloaded 97 times
(24.52 KiB) Downloaded 98 times
(99.86 KiB) Downloaded 102 times

User avatar
Posts: 13804
Joined: Tue May 22, 2007 9:57 am

Re: GUI project going grey, (Not Responding)

Post by jvierra » Sun Feb 07, 2016 5:17 pm

I can only recommend that you palce trace statements in the code and run it. Just output to a regular file for all of your write-debug lines. It is likely that assumptions made running in the IDE do not hold when running as a script. Try generating script to file and running the script in a CLI version of PS.

User avatar
Posts: 2
Joined: Mon Feb 01, 2016 5:11 pm

Re: GUI project going grey, (Not Responding)

Post by o0beaner » Tue Feb 09, 2016 10:04 am

Alright, so I figured this out.

My issue was actually related to calling a SaveFileDialog, and my compiler was not using STA mode, which SAPIEN states is required for that control[1].

This actually brings me to another question, however, still related to the same thread subject.

I previously mentioned that I am leveraging a modified version of the JobTracker control set, and nearly all of my script actions are run as background jobs. That being said, there is a point where I need to wait for a number of background jobs to complete before script actions continue. The only way that I know how to do this would be wait-job, but that causes the gui form to hang/switch to not responding for the duration of the wait-job. Technically it's fine, but its a poor user experience.

I have a feeling that wait-job is the 'wrong' way to do this. Do you happen to have any alternative suggestions for waiting on jobs to complete without hanging the form?

[1]: ... -sta-mode/