Vscode-powershell: System.Windows.Forms Dialogs Don't Open In Debugger

Created on 22 Dec 2016  路  16Comments  路  Source: PowerShell/vscode-powershell

Please fill in these details so that we can help you!

System Details

  • Operating system name and version: Windows 10 x64
  • VS Code version: 1.8.1
  • PowerShell extension version: 0.8.0
  • Output from $PSVersionTable:
Name                           Value
----                           -----
PSVersion                      5.0.10586.672
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.672
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

Using the below script in the debugger, the dialog window does not appear, and the debugger crashes.

function Get-FileName($initialDirectory){   
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |  Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "All files (*.*)| *.*"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

Attached Logs

vscode-powershell - Copy.txt
EditorServices - Copy.txt

Issue-Bug

Most helpful comment

@SuHwak IIRC there is a bug in Electron where dialogs were showing up behind VSCode. Can you double check that the dialog you're looking for isn't behind VSCode.

All 16 comments

Hmm, haven't seen this before. I'll take a look!

A little more info... I changed the code to this (using Add-Type instead of [System.Reflection...]) and debugged through it... it won't progress past the ".ShowDialog()" and never actually shows the dialog.

function Get-FileName($initialDirectory){   
    Add-Type -AssemblyName System.Windows.Forms

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "All files (*.*)| *.*"
    $result = $OpenFileDialog.ShowDialog()
    if($result -eq "Cancel"){
        break
    }
    return $OpenFileDialog.filename
}

$path = Get-FileName "c:\aaa"

I do know that some of the windows forms stuff works, because I can use the below code to pop up a message balloon:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$message = "test"
#Notification Balloon
#load Windows Forms and drawing assemblies           
#define an icon image pulled from PowerShell.exe            
$icon=[system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe)) 

$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 

$objNotifyIcon.Icon = $icon
$objNotifyIcon.BalloonTipIcon = "Info"  #can be Info, Warn, or Error
$objNotifyIcon.BalloonTipText = "$message" 
$objNotifyIcon.BalloonTipTitle = "Info"

$objNotifyIcon.Visible = $True 
$objNotifyIcon.ShowBalloonTip(10000) #time in seconds to show notification

A suggestion for fixing this (maybe a quick fix?)... for C# projects, there is an attribute in the launch.json called externalConsole (see screenshot below) that allows you to debug in an external window. Not sure if that would be possible for powershell, but maybe worth looking at.

image

Unfortunately this setting will not make a difference, it's more of an issue with the code in our debug adapter. I still haven't got a chance to look at this but I will as soon as I ship 0.9.0. Sorry for the delay!

Hi, is there any plan to fix this? Working on a project that uses this a lot, and it is a pain switching over to ISE to debug...

I ran into this myself on a longtime running script that prompts for user/password:
$Host.ui.PromptForCredential($WindowTitle, $Prompt, $UserName, "", $CredentialType, $ValidateOption)

On ISE or commandline this creates a windows box that prompts. With the current terminal, it prompts in plain text.

I think @sunmorgus has a reasonable idea. You can work-around this by launching your script in an external console with a Wait-Debugger at the start, use VSCode to attach to the process and then debug it. However, we should provide an easier way to do this i.e. debug via a new, external console.

I am in the process of moving over from ISE to VS Code (to leverage git), only to come across this problem. @rkeithhill Do you have a step-by-step for me?

I can't repro the original issue. The OpenFile dialog opens for me and returns the selected file. If you have a repro post it here otherwise, I'm going to close this issue.

Below is the code I'm working with (mostly not my own), which works great in PowerShell_ISE, as well as a regular PS prompt.

I just found that if debugging with the option to "Launch current file in temporary console" it works too, however, when using the default "Launch current file" it executes in the integrated console and it just hangs indefinitely.

Using the temporary console doesn't work for debugging blocks or lines of code of course.

Is there a logfile for VS Code? I'm running the latest version 1.24.0 on Windows 10 1803.

Function Get-FileName($InitialDirectory, $Filename, $FilenameDescription ,$FileType, $FileTypeDescription, $Title)
{  
 [Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

 if (!$Filename) 
    {
    $Filename = "*"
    $FilenameDescription = "Any file name of type"
    }

 if (!$FileType) 
    {
    $FileType = "*"
    $FileTypeDescription = "any"
    }


 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.Title = $Title
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "$($FilenameDescription) $($FileTypeDescription) ($($Filename).$($FileType)) | $($Filename).$($FileType)"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName

$PickedFile = Get-FileName

Write-Host $PickedFile

@SuHwak you should be able to find the log files by following these instructions.

The other way to do it is to go into VSCode and use Ctrl+Shift+P to bring up the VSCode command palette. In there, if you type "logs" you should get presented with an option like "PowerShell: Open PowerShell Extension Logs Folder". That will open the folder up for you in VSCode.

Thank you @rjmholt, however, the logs don't show anything because the console just hangs (probably waiting for the result), and I have to force close the terminal. Any other suggestions?

@SuHwak the logs should exist if the integrated console started 鈥斅燼re you saying there are no logs at all, or that they just drop off when you run the hanging code and there's no further info?

@SuHwak IIRC there is a bug in Electron where dialogs were showing up behind VSCode. Can you double check that the dialog you're looking for isn't behind VSCode.

@rjmholt @tylerl0706 It was indeed hiding stealthily behind VS Code window all that time. It didn't create a taskbar item either or else I would have noticed earlier! Thank you, I can now revert back (thanks to Git) :)

Here's the issue on VSCode.

You may want to reply in that issue that this is the case.

Im going to close this in favor of that since there's nothing we can do. If you think we do have a problem here, feel free to reach out!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukegriffith picture lukegriffith  路  3Comments

rkeithhill picture rkeithhill  路  3Comments

TheDanishDynamo picture TheDanishDynamo  路  3Comments

nathan-alden-hp picture nathan-alden-hp  路  3Comments

AWahlqvist picture AWahlqvist  路  3Comments