GUI hang when specific script engine is chosen

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 5 years and 7 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
milindnaphade
Posts: 10
Last visit: Thu Apr 21, 2022 11:00 pm

GUI hang when specific script engine is chosen

Post by milindnaphade »

I have been working on a GUI project recently. The GUI contains a while loop that determines the copy progress.
  1.             Start-AzureStorageBlobCopy -AbsoluteUri $SourceVHD.ToString() -DestContext $FStorageContext -DestContainer $FContainer.Name.ToString() -DestBlob $FBlob.ToString()
  2.             $txtResult.AppendText("Copy job queued. Updates will begin shortly. `n")
  3.             $FCopyStatus = Get-AzureStorageBlobCopyState -Context $FStorageContext -Blob $FBlob -Container $FContainer.Name
  4.             While ($FCopyStatus.Status -eq "Pending")
  5.             {
  6.                 $FCopyStatus = Get-AzureStorageBlobCopyState -Blob $FBlob -Container $FContainer.Name -Context $FStorageContext
  7.                 $PercentageComplete = (($FCopyStatus.BytesCopied/$FCopyStatus.TotalBytes) * 100)
  8.                 $PercentageComplete = [System.Math]::Round($PercentageComplete, 0)
  9.                 $StatusText = [string]::Format("Copying {0} % complete{1}", $PercentageComplete, [System.Environment]::NewLine)
  10.                 $txtResult.AppendText($StatusText)
  11.                 Start-Sleep -Seconds 30
  12.             }
The GUI hangs exactly at this code block when it is built with Powershell V5 Windows Forms or any other script engine except the Windows Application script engine. Everything works perfect in debug mode in the powershell studio 2018, but when compiled into an exe using Windows Forms or Windows Script engine.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI hang when specific script engine is chosen

Post by jvierra »

Yes. A loop will always hang a form. A form in PowerShell has only one thread. A loop blocks that thread and freezes the form.

See: https://info.sapien.com/index.php/guis/ ... sive-loops
User avatar
milindnaphade
Posts: 10
Last visit: Thu Apr 21, 2022 11:00 pm

Re: GUI hang when specific script engine is chosen

Post by milindnaphade »

Thank you for your suggestion, and I am sorry that I have not been clear about what is happening. The issue isn't the loop. The loop executes and works well if the script engine is set to "Windows Application." The same code fails to execute when the script engine is set to Windows Forms for Windows.
I am trying to compile the GUI into an executable. My question is, how is it possible that the entire code works correctly when the script engine is set to Windows Application and fails to execute the block when the script engine is changed to other two options I mentioned earlier.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI hang when specific script engine is chosen

Post by jvierra »

See the following to learn how to best use Azure with Forms: https://www.sapien.com/blog/2018/07/19/ ... ound-jobs/
User avatar
milindnaphade
Posts: 10
Last visit: Thu Apr 21, 2022 11:00 pm

Re: GUI hang when specific script engine is chosen

Post by milindnaphade »

jvierra wrote: Fri Jul 20, 2018 1:46 am See the following to learn how to best use Azure with Forms: https://www.sapien.com/blog/2018/07/19/ ... ound-jobs/
Hello, I have gone through almost every article that talks about how to use the GUI and scripts appropriately. Assuming that we understand the form may hang during a long-running loop or script, is there a way to find out why one build engine fails to work properly while the other one fails?

Again, we are okay with a hung GUI while the scripts are executing in the background.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI hang when specific script engine is chosen

Post by jvierra »

The article explains how to use Azure with Forms. You need to understand the article.

Forms must be built with the Forms engine. Azure Cmdlets may require WMF 5.x.

Many Azure CmdLets can only be run in a Job as they will conflict with Forms. That is the point of the blog article.

Be sure you have the latest AzureRM module installed.
User avatar
milindnaphade
Posts: 10
Last visit: Thu Apr 21, 2022 11:00 pm

Re: GUI hang when specific script engine is chosen

Post by milindnaphade »

Well, The interesting thing is it all worked until I came here to post the issue. I am quite sure that I am not doing anything wrong. Is there anyone at Sapien who can help with this issue. It is nothing to do with the cmdlets and how they are executed and all. The challenge seems to be that once the script is built using any engine other than "Windows Application" Start-AzureStorageBlobCopy does not even get executed. On the other hand, everything seems absolutely fine when I run the script in Debug mode in the IDE.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI hang when specific script engine is chosen

Post by jvierra »

Are you running the Azure CmdLets a with "AdJob" as described in the link I posted? Most Azure CmdLets will not work in a From unless they are run with the "AsJob" parameter which would fully explain the issues you are having.

If you need Sapien support for assistance then post here: viewforum.php?f=12

This forum is not a Sapien support forum. It is a community support forum manned by volunteers and as such we cannot help with internal PowerShell Studio issues. The posts and answers above are from my own experience with Azure and Power Shell when running in a form.

Try reviewing the link first and try using the jobs as shown in the examples. Run the examples until you understand how it works then try your own code.
User avatar
milindnaphade
Posts: 10
Last visit: Thu Apr 21, 2022 11:00 pm

Re: GUI hang when specific script engine is chosen

Post by milindnaphade »

Hi Jvierra

I understand the use of Jobs quite well. The challenge is that the updates from the jobs need to be constantly shown in the GUI which is making it really difficult. I think it is an issue with the script engine more than the code. The compiled script hangs at the cmdlet Start-AzureStorageBlobCopy which cannot be run using -AsJob parameter. I will try the Sapien support link that you posted.
Another reason I am sounding reluctant to change the code to is due to the fact that the loops are not hanging. A simple cmdlet is not getting past the execution.
  1. Start-AzureStorageBlobCopy -AbsoluteUri $SourceVHD -DestContext $FStorageContext -DestContainer $FContainer.Name -DestBlob $FBlob
  2.             $txtResults.AppendText("Copy job queued. Updates will begin shortly.`n")
  3.             $FCopyStatus = Get-AzureStorageBlobCopyState -Context $FStorageContext -Blob $FBlob -Container $FContainer.Name
  4.             [b]$txtResults.AppendText("Entering the While Loop......`r`n")[/b]
  5.              While ($FCopyStatus.Status -eq "Pending")
  6.  
  7. .......
The bold line never displays in the text box. So it is clear that the engine somehow does not know how to handle it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI hang when specific script engine is chosen

Post by jvierra »

I see no bold line.
All azure commands need to be run in a single job. Job results are only available by using Receive-Job.
Axure commands not run in a job will hang and the form will also hang.

Please read the link carefully. If you do not understand what it is trying to tell you then ask questions about what it is you don't understand about the blog article.
This topic is 5 years and 7 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