Page 1 of 1

GUI project going grey, (Not Responding)

Posted: Sun Feb 07, 2016 3:47 pm
by o0beaner
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:
  1. $main_button_run_Click={
  2.     Write-Debug 'run clicked'
  3.     $main_label_elapsed_static.Visible = $false
  4.     $main_label_elapsed.Visible = $false
  5.     #DiagRun
  6.     LogRun-Setup
  7. }
The function being called is stored in globals.ps1, and does the following:
  1. function LogRun-Setup
  2. {
  3.     # Checks for presence of security group, creates/attaches if necessary.
  4.     # A PS session is opened for the LogRun task, the root working path is
  5.     # set and the start time is stored. A folder is created at the root
  6.     # working path.
  7.    
  8.     Write-Debug 'Logrun-Setup init'
  9.     $main_label_status.text = "Creating security group..."
  10.     Write-Debug 'CheckSG'
  11.     CheckSG
  12.     $main_label_status.text = "Opening session..."
  13.     $global:jobList = @()
  14.     $global:startDTM = (Get-Date)
  15.     $global:RootPath = "c:\WorkSpacesLogs\"
  16.     $main_label_status.text = "Creating working directory..."
  17.     Write-Debug 'adding rootpath job'
  18.     Add-RemoteJobTracker -Name "CreateRootPath" -JobScript `
  19.     {
  20.         $global:RootPath = $args[0]
  21.         Remove-Item $global:RootPath -recurse -Force -ErrorAction SilentlyContinue | out-null
  22.         New-Item $global:RootPath -type directory -force | out-null
  23.     } -ArgumentList $global:RootPath -CompletedScript `
  24.     {
  25.         Write-Debug "RootpathCreated"  
  26.     }
  27.     Wait-Job -Name "CreateRootPath"
  28.     Write-Debug "LogRun-Setup Complete"
  29.     $main_label_status.text = "Gathering Workspace Logs..."
  30. }
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?

Re: GUI project going grey, (Not Responding)

Posted: Sun Feb 07, 2016 4:17 pm
by jvierra
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.

Re: GUI project going grey, (Not Responding)

Posted: Tue Feb 09, 2016 9:04 am
by o0beaner
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]: https://www.sapien.com/blog/2015/03/09/ ... -sta-mode/