locked
PowerShell ISE Freezes after executing WinForms RRS feed

  • Question

  • Hello, I've been having an issue with my PowerShell ISE. I am currently developing a PowerShell script that generates Windows Forms for gathering and displaying data. From what I can tell, the application runs and works great. However, after I execute the code once in the ISE (and close the app/stop execution gracefully); if I leave the ISE idle, for about 10 minutes, the ISE will freeze and I have to crash it with Task Manager. If winform code has not been executed, ISE works fine indefinitely. Thoughts on why my ISE constantly crashes when working on winform based scripts? Thanks.
    • Edited by bcehr Tuesday, July 28, 2015 3:17 PM
    Tuesday, July 28, 2015 3:17 PM

All replies

  • For break/fix contact Microsoft support. 


    \_(ツ)_/

    Tuesday, July 28, 2015 4:37 PM
    Moderator
  • jrv, I have been attempting to create tickets with Microsoft Support for this issue but have gotten no support from Microsoft. They claim it is a developer problem, even though it appears to definitely be a bug with the ISE (see link below).  Thoughts on how I can better approach this? 

    Here is a link with others with this same issue:

    http://stackoverflow.com/questions/30808084/using-windows-forms-locks-up-powershell-ise-minutes-after-script-has-terminated


    • Edited by bcehr Monday, August 31, 2015 1:49 PM
    Monday, August 31, 2015 1:49 PM
  • Hi there.

    I am building a little program to implement easier GUIs for/in Powershell as readable script.

    I can´t even count how many times the ISE crashed. By my experiences the crashes occured on debugging something more complicated. My motivation was not very high to find the error because it did not disturb my work too much and after a restart all scripts were reloaded and afterwards it worked for some hours again.

    Though - yeah. Debugging XAML or WinForms causes often Crashes in ISE.


    Best regards,

    David das Neves

    Technology Specialist - Consulting Services
    Computacenter AG & Co. oHG - Munich

    Blog    

    Caution: This post was written with the intention to help and may contain errors.
    If my post was helpful or answered your question please mark it respectively. Thanks.

    Monday, August 31, 2015 1:53 PM
  • Yeah, smaller winform programs haven't been a huge issue, they can work for several hours at a time. But my bigger scripts, containing say 3000 lines with dozens of winform objects, are crashing every few minutes; dozens of times a day and costing a lot of time (not to mention I keep losing all my test variables in my active shell). It seems the large the scripts, the faster the crashes.
    Monday, August 31, 2015 2:01 PM
  • Oh well.

    You could gather a dump of the ISE with ProcDump to see why this is crashing on your site so often. Probably we can find a hint so the ISE/error can be fixed.

    Link    ProcDump


    Best regards,

    David das Neves

    Technology Specialist - Consulting Services
    Computacenter AG & Co. oHG - Munich

    Blog    

    Caution: This post was written with the intention to help and may contain errors.
    If my post was helpful or answered your question please mark it respectively. Thanks.

    Monday, August 31, 2015 2:18 PM
  • The ISE is an editor. It is not really intended to be used in production.  Use the CLI version of PowerShell to run WinForms.


    \_(ツ)_/

    Monday, August 31, 2015 3:19 PM
    Moderator
  • Jrv, yeah i'm using the ISE as a code editor. Once the script goes into production, it will be launched with the CLI version of PowerShell.  However, the crashing that is plaguing me comes from while actively writing and debugging the code in the ISE. When I run the script in order to test/debug it, PowerShell ISE crashes shortly after the debug session/test run is complete.  This requires me to crash and re-launch ISE nearly every time I test a new line of code...
    Monday, August 31, 2015 3:57 PM
  • This is an issue with debugger.  All IDEs have this issue. It says you have a very subtle bug in your code.  Possible a leak or something that is corrupting the Windows structures.    It happens to all developers.

    You need to break your script apart and test pieces until you have found the issue.  If you are not experienced in design of Windows code then you will likely have serious design issues on a large project.

    Remember that all event code must be cared for carefully.  I have seen code where a user left a timer running which crashed to system after the Window closed.

    In CLI with a script file you never would see the lockup because PowerShell shuts down before the timer ticks.  In ISE the coode ends but the GUI remains. A timer tick or other late event will now lock the GUI.


    \_(ツ)_/

    Monday, August 31, 2015 4:30 PM
    Moderator
  • Jrv, thanks for the reply.  That was my original thought as well, but I literally went through the code and did a manual Remove-Variable for EVERY object in the code to cleanup and terminate everything and the ISE still freezes.  Running Get-Variable | Remove-Variable after script execution also still results in the ISE locking up after a short time.  I have also read other users having this same problem with even a simple winform application with a simple form with one control on it.  Wondering if possibly could be a bug in the ISE in relation to winforms? Or there something else in my code I could be missing?


    • Edited by bcehr Monday, August 31, 2015 6:41 PM
    Monday, August 31, 2015 6:40 PM
  • Every case I have seen it was a coding error.  If you doubt that then open a case with MS support.  If they find a bug the case will be free.

    I do not recommend that non-programmers build complex GUI apps with PowerShell. IF you run into a crisis you will find it very difficult to find the cause as you are seeing.

     


    \_(ツ)_/

    Monday, August 31, 2015 10:21 PM
    Moderator
  • Yeah, I have ran the script many times in the PowerShell console, leaving the console open with all variables intact for hours/days, the console never crashes, hangs, etc. However, the ISE will freeze within minutes, this should confirm there is a bug with the ISE?  Also many others out there have reported similar issues with the ISE when working with XML, WinForms and other type objects. I have tried again with MS support, but so far they are refusing to even look at the issue. Thanks!
    • Edited by bcehr Thursday, September 3, 2015 11:50 AM
    Thursday, September 3, 2015 11:49 AM
  • Hi,

    This isn't exactly a solution to your actual issue, but have you tried alternate editors as a bandaid fix? I don't really do anything with forms, so I've had no issues with the ISE, but there are people out there who really seem to love PowerGUI.

    Or PowerShell Studio, if you're willing to pony up for it.


    Thursday, September 3, 2015 11:59 AM
    Moderator
  • MS support will always look at the issue when you pay for support.

    I will stress that this is not likely a bug.  Without your script it is impossible for anyone to even guess at what you are doing.

    There are specific differences between the ISE and the CLI.  ISE is a GUI based editor and debugger.  It is not a target production  execution environment.


    \_(ツ)_/

    Thursday, September 3, 2015 3:02 PM
    Moderator
  • Mike, I downloaded and tested the script with PowerGUI and ran into no problems. I ran the script in the PowerGUI Script Editor an entire day without a single crash. I guess this further supports that there is a core issue with PowerShell ISE when working with certain types of scripts since both PowerGUI and PowerShell ISE are GUI based editor and debuggers.
    Friday, September 4, 2015 12:28 PM
  • +1. I have the same issue.

    After running the code below, the ISE stops to respond after 5-10 minutes.

    function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
    {

        Add-Type -AssemblyName System.Drawing
        Add-Type -AssemblyName System.Windows.Forms
         
        # Create the Label.
        $label = New-Object System.Windows.Forms.Label
        $label.Location = New-Object System.Drawing.Size(10,10)
        $label.Size = New-Object System.Drawing.Size(280,20)
        $label.AutoSize = $true
        $label.Text = $Message
         
        # Create the TextBox used to capture the user's text.
        $textBox = New-Object System.Windows.Forms.TextBox
        $textBox.Location = New-Object System.Drawing.Size(10,40)
        $textBox.Size = New-Object System.Drawing.Size(575,200)
        $textBox.AcceptsReturn = $true
        $textBox.AcceptsTab = $false
        $textBox.Multiline = $true
        $textBox.ScrollBars = 'Both'
        $textBox.Text = $DefaultText
         
        # Create the OK button.
        $okButton = New-Object System.Windows.Forms.Button
        $okButton.Location = New-Object System.Drawing.Size(415,250)
        $okButton.Size = New-Object System.Drawing.Size(75,25)
        $okButton.Text = "OK"
        $okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
         
        # Create the Cancel button.
        $cancelButton = New-Object System.Windows.Forms.Button
        $cancelButton.Location = New-Object System.Drawing.Size(510,250)
        $cancelButton.Size = New-Object System.Drawing.Size(75,25)
        $cancelButton.Text = "Cancel"
        $cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
         
        # Create the form.
        $form = New-Object System.Windows.Forms.Form
        $form.Text = $WindowTitle
        $form.Size = New-Object System.Drawing.Size(610,320)
        $form.FormBorderStyle = 'FixedSingle'
        $form.StartPosition = "CenterScreen"
        $form.AutoSizeMode = 'GrowAndShrink'
        $form.Topmost = $True
        $form.AcceptButton = $okButton
        $form.CancelButton = $cancelButton
        $form.ShowInTaskbar = $true
         
        # Add all of the controls to the form.
        $form.Controls.Add($label)
        $form.Controls.Add($textBox)
        $form.Controls.Add($okButton)
        $form.Controls.Add($cancelButton)
         
        # Initialize and show the form.
        $form.Add_Shown({$form.Activate()})
        $form.ShowDialog() > $null   # Trash the text of the button that was clicked.
         
        # Return the text that the user entered.
        return $form.Tag
    }

    Wednesday, January 27, 2016 8:07 AM
  • We keep hearing this claim over the years.  No one has been able to reproduce this,  There are likely other things you are running before the dialog is loaded.  Perhaps in the profile.

    \_(ツ)_/

    Wednesday, January 27, 2016 8:18 AM
    Moderator
  • Here is a less complicated version of your dialog.

     function Read-MultiLineInputBoxDialog{
        Param(
            [string]$Message='Enter your message',
            [string]$WindowTitle='Enter Some Text',
            [string]$DefaultText
        )
    
        #Add-Type -AssemblyName System.Windows.Forms
          
        # Create the Label.
        $label = New-Object System.Windows.Forms.Label
        $label.Location ='10,10'
        $label.Size ='280,20'
        $label.AutoSize = $true
        $label.Text = $Message
          
        # Create the TextBox used to capture the user's text.
        $textBox = New-Object System.Windows.Forms.TextBox 
        $textBox.Location ='10,40'
        $textBox.Size ='575,200'
        $textBox.AcceptsReturn = $true
        $textBox.AcceptsTab = $false
        $textBox.Multiline = $true
        $textBox.ScrollBars = 'Both'
        $textBox.Text = $DefaultText
          
        # Create the OK button.
        $okButton = New-Object System.Windows.Forms.Button
        $okButton.Location ='415,250'
        $okButton.Size ='75,25'
        $okButton.Text = "OK"
        $okButton.DialogResult='Ok'
          
        # Create the Cancel button.
        $cancelButton = New-Object System.Windows.Forms.Button
        $cancelButton.Location ='510,250'
        $cancelButton.Size ='75,25'
        $cancelButton.Text = "Cancel"
        $cancelButton.DialogResult='CAncel'
          
        # Create the form.
        $form = New-Object System.Windows.Forms.Form 
        $form.Text = $WindowTitle
        $form.Size ='610,320'
        $form.FormBorderStyle = 'FixedSingle'
        $form.StartPosition = "CenterScreen"
        $form.Topmost = $True
        $form.ShowInTaskbar = $true
          
        # Add all of the controls to the form.
        $form.Controls.Add($label)
        $form.Controls.Add($textBox)
        $form.Controls.Add($okButton)
        $form.Controls.Add($cancelButton)
          
        # Initialize and show the form.
        $form.Add_Shown({$form.Activate()})
        
        if('Ok' -eq $form.ShowDialog()){ $textbox.Text }
    
    }
    

    Notice how much easier it is to return the results.


    \_(ツ)_/



    • Edited by jrvModerator Wednesday, January 27, 2016 8:40 AM
    Wednesday, January 27, 2016 8:32 AM
    Moderator
  • Yeah, I have reproduced it easily. I find the more winform objects included, the easier/more likely it is to reproduce. But I have reproduced with code this simple:

    $Form = New-Object System.Windows.Forms.Form
    $Form.DataBindings.DefaultDataSourceUpdateMode = 0
    $Form.Text = "My Form"
    $Form.AutoSize = $true
    $Form.AutoSizeMode = "GrowAndShrink"
    $Form.StartPosition = "CenterScreen"
    $Form.MaximizeBox = $false
    $Form.FormBorderStyle = "FixedSingle"

    $Return = $Form.ShowDialog()

    This was reproduced with fresh ISE console and no scripts in my profile.  Other ISEs besides the MS one, do not have the same issue.

    Wednesday, January 27, 2016 2:45 PM
  • I and others cannot reproduce this on and ISE console.


    \_(ツ)_/

    Wednesday, January 27, 2016 3:34 PM
    Moderator
  • I can confirm this issue. Only freezes after I have ran a Winform and letting it be idle for 5-10 minutes (i.e. bathroom break). I come back and ISE is frozen and I have to force close.
    Friday, February 5, 2016 3:34 PM
  • I can confirm this issue. Only freezes after I have ran a Winform and letting it be idle for 5-10 minutes (i.e. bathroom break). I come back and ISE is frozen and I have to force close.

    This is not the correct place to report bugs.

    \_(ツ)_/

    Friday, February 5, 2016 4:57 PM
    Moderator
  • This is not the correct place to report bugs.

    \_(ツ)_/

    For those interested, the correct place is here:

    http://windowsserver.uservoice.com/forums/301869-powershell

    The PowerShell team has abandoned Connect and moved over to UserVoice.


    Friday, February 5, 2016 5:05 PM
    Moderator
  • FYI, I created a post on the user voice here:

    http://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11733891-powershell-ise-crashes-after-loading-winforms

    Feel free to vote it up.

    Friday, February 5, 2016 5:14 PM
  • tcox8, glad to hear another person fighting this. Also just FYI, I did try to bring this up to MS support multiple times to get bug fixed by Dev, but never got anywhere. They would either call it a programming problem (bad code, which is false in this case), or told me to not use winforms...which is also useless feedback...

    • Edited by bcehr Friday, February 5, 2016 5:16 PM
    Friday, February 5, 2016 5:16 PM
  • Yeah it is all over the internet as well. Here is a bug report on this from the Connect site. (I am also commenting this on your bug report.

    https://connect.microsoft.com/PowerShell/Feedback/Details/1224213

    You can cause the freeze by simply changing your resolution because the actual root cause is happening when it receives a WM_SETTINGCHANGE broadcast message.

    Friday, February 5, 2016 5:19 PM
  • So far no one has posted any code that I or others have tried that can reproduce the bug.

    If you can produce an example with explicit directions that can reproduce the issue then you will more likely find an answer.


    \_(ツ)_/


    Friday, February 5, 2016 5:37 PM
    Moderator
  • I believe you are failing to read? Code is provided above.

    I can replicate this with any code using WimForms. In fact I even did it with the code that you (jrv) provided above (you simplified someone else's code). After I run that I can reproduce the ISE Freezing by either letting my password enabled screen saver kick on (going idle) or by changing the resolution on my screen.

    Friday, February 5, 2016 5:42 PM
  • I am also able to completely reproduce the issue using the code above. It works great in PowerShell 2 (Windows 7) but freezes in PowerShell 5 (Windows 10) almost immediately and never returns the values being entered into the text box.... even causes a blue screen if I keep force closing PowerShell and running it again in windows 10.

    NOW instead of using all those lines of code to make an input box, this one liner works.... but it does give errors if you hit cancel ect, so not as good for non standard input...probably an easy way to fix this, but this line works in windows 10:

    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null

    $group = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a Group name", "Group", "$env:groupname")


    Monday, April 4, 2016 4:09 PM
  • So far no one has posted any code that I or others have tried that can reproduce the bug.

    If you can produce an example with explicit directions that can reproduce the issue then you will more likely find an answer.


    \_(ツ)_/


    JRV - please read the entire thread before you continue to post.  This error can be reproduced using the code provided as well as the same "cleaned up" code of somebody else's that you modified and posted.  This is definitely a ISE issue when opening WinForms so please stop saying it is due to the OP's code.  None of your responses to this thread have been helpful so please - if you do not wish to assist with an answer, do not reply to the thread.  Even after your "This is not the correct place to report bugs" comment, you didn't even provide a link to the site where bugs are reported - someone else was gracious enough to provide that link to the OP.  You may be a programmer - but so am I along with MANY others who read these forums.  To tell someone "I do not recommend that non-programmers build complex GUI apps with PowerShell." is a very brash statement and uncalled for as it is your opinion.

    To everyone else - sorry for the rant.  It just bothers me when people troll forums and post replies to make themselves look like they are the top expert in a technology and yet they do not provide any valuable information or feedback.  bcehr, you are correct that the ISE appears to have issues with WinForms...  You are NOT alone as JRV previously tended to make it seem.  I too have several PowerShell scripts that use WinForms and if I run them for testing inside of the ISE, shortly afterward the ISE locks up and can only be closed by doing a CTRL+ALT+DELETE and ending the powershell_ise.exe task.  The issue only occurs when running a PSS that uses WinForms when using the ISE but not when using PowerGUI.  I have read that others who experienced the same issue with PowerShell 3.0 removed PowerShell completely and reinstalled it which seems to fixed the issue for them.  Again - the removal and reinstallation was specific to PowerShell 3.0, so your results may vary.  Here's a link to a thread at powershell.com if you are interested:  http://powershell.com/cs/forums/t/20999.aspx

    Please continue to update this thread with any progress (or lack thereof) you experience.  Hopefully we can find a solution to this issue.

    Monday, April 11, 2016 8:41 PM
  • I have found no code that can reproduce this.  I have tried all bits posted and none cause a freeze.  I suspect there is something installed or missing on the systems that cause issues.

    I have just retested on a brand new HP laptop with Windows 10Pro.  I cannot cause a lockup with any Winform that is written correctly.

    What is this PSS you are referring to?


    \_(ツ)_/

    Monday, April 11, 2016 8:51 PM
    Moderator
  • JRV - please read the entire thread.  There are multiple instances and examples listed, one of them being your repost of someone else's code that you modified.
    Monday, April 11, 2016 9:12 PM
  • JRV - please read the entire thread.  There are multiple instances and examples listed, one of them being your repost of someone else's code that you modified.

    It have never gotten any one of the examples to cause a freeze.

    Don't worry about this.  It will not be a problem to me or most people.  Ty and find out what is installed that is causing an issue.  WMF5 preview is known to cause many issues.


    \_(ツ)_/

    Monday, April 11, 2016 9:27 PM
    Moderator
  • Tell the original OP or anyone else who is experiencing the issue "Don't worry about this".

    I do not think you have read the entire thread, so let me quickly recap for you.

    Run the code examples listed in this thread that open WinForms in the ISE.

    Allow the ISE to remain open after the PSS is complete.

    Have a password protected screen saver kick in or change the resolution on your monitor.

    The ISE freezes and can only be closed by End Task.

    I can reproduce this issue every time.

    Monday, April 11, 2016 9:33 PM
  • been there done that nd it doesn't freeze.  Sorry.  I am just saying that I cannot reproduce any of these things.

    I have an older laptop upgraded to WMF 5 while it was W8.1.  After the W10 upgrade PowerShell ahs been very bad.  It freezes often.  I have just moved everything to a new system so I can now do a full W10 install with a format.  I bet it doesn't freeze after the upgrade.

    There are many things that can cause a freeze,  Mostly it is a deadlock.  Third party screen savers were a big bad thing in WXP and earlier. AV software can also cause a deadlock. 

    The best you can do is post the bug review request and wait.  It has been posted for a very long time now and nothing has been found.


    \_(ツ)_/

    • Proposed as answer by Rocky-015 Wednesday, September 21, 2016 2:04 PM
    • Unproposed as answer by Rocky-015 Wednesday, September 21, 2016 2:04 PM
    Monday, April 11, 2016 9:39 PM
    Moderator
  • Looking through this thread, I myself have powershell ISE Freezing like everyone else on this thread. It is not because of incorrect code as some may jump at the chance to say. It is a problem with ISE itself. at the beginning, I also thought it may be my code. I ran it and would freeze once a week or so, except Now it freezes within 10 minutes of starting it. I know it is not the code because it is doing it before even running the code now. I was told a possible fix is to uninstall and reinstall powershell. Instead, I have taken to using PowerGUI until I get more time to troubleshoot the issue.
    Wednesday, September 21, 2016 2:31 PM
  • Same issue here, same conditions. Run a script with WinForms and after a period of inactivity I go back to it and ISE is completely frozen out. Huh.
    Wednesday, September 28, 2016 12:36 PM
  • Hi, 

    I am experiencing the same issue. I have to kill the PS process. This is in a Windows 10 1607.

    Thanks,


    • Edited by Mr.Vacano Saturday, October 15, 2016 2:48 PM
    Saturday, October 15, 2016 2:47 PM
  • Try this for execution:

    $window.Dispatcher.InvokeAsync{$window.ShowDialog()
    }.Wait() 
    

    This should be much more stable.


    Best regards,

    David das Neves
    Premier Field Engineer



    Please vote as helpful and mark as answer if a post helped you.
    This posting is provided "AS IS" with no warranties and confers no rights.

    Wednesday, October 26, 2016 8:21 AM
  • Can you expand on this please.
    Friday, February 3, 2017 6:35 PM
  • I opened a Premier case with Microsoft on this as it is reproducible on many different machines and OS versions.  After sending them some debug logs they confirmed that it is an issue and are working to find a resolution and a workaround.  This KB describes why it is happening: https://support.microsoft.com/en-us/help/943139/windows-forms-application-freezes-when-system-settings-are-changed-or

    The bad part is they have not come up with a workaround for PowerShell.  It gets worse because if you use any Microsoft Online/Azure modules and have MFA enabled for your account the MFA prompt will cause the same problem as it appears to also use WinForms. 

    Monday, August 7, 2017 9:05 PM
  • Good information but it will likely never be fixed for ISE.  The issue is due to an incorrect deployment of a form as noted in the article.  This is fundamental Windows behavior.  ISE is multi-threaded where CLI is not.  There is no way to force a form onto the main thread of the ISE.  The ISE suffers from this and other deficiencies with forms and events. 

    I believe you can work around this with ISE by launching the form in an external shell.  The external shell will have only one thread and should not cause the deadlock.


    \_(ツ)_/

    Monday, August 7, 2017 9:16 PM
    Moderator
  • Thanks for opening the Premier case! Let us know what the results of it are. Yeah, the MS Online/Azure modules constantly freeze my PowerShell all day long due to the MFA prompts...its extremely frustrating when trying to work with Azure...
    Monday, August 7, 2017 9:23 PM
  • It would be useful to know exactly what platforms the Forms example freezes on.  I have not been able to reproduce this on Windows 10 (any build) but have not tried it on others. I also don't see how this follows the Azure issue which appears to be running a dialog on a separate thread.  A form launched with the little green button does not freeze. Perhaps it is specific to how you are launching the form from ISE.


    \_(ツ)_/

    Monday, August 7, 2017 9:36 PM
    Moderator
  • It has been happening to be everyday for years on all builds of Windows 10 Enterprise x64 (RTM-1703)
    Monday, September 11, 2017 2:39 PM