Skip to content

Instantly share code, notes, and snippets.

@altrive
Created August 14, 2013 01:16
Show Gist options
  • Save altrive/6227237 to your computer and use it in GitHub Desktop.
Save altrive/6227237 to your computer and use it in GitHub Desktop.
PowerShell crash when calling ShowDialog

Summary

PowerShell crash when calling ShowDialog for the second time. This error occurred when leaving PowerShell console for a long time(10-20 minutes)

http://connect.microsoft.com/PowerShell/feedback/details/691026/trying-to-showdialog-crashes-powershell

Minimal code for reproduce

#Load WPF assembly for Powershell.exe
Add-Type -AssemblyName PresentationFramework

#Create WPF Window object
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
    </Grid>
</Window>
"@

#First call(Need to close window manually)
$window = [Windows.Markup.XamlReader]::Parse($xaml)
$window.Topmost = $True  
$window.ShowDialog()

#Wait 20 minutes
sleep (60 * 20) 

#Second call
$window = [Windows.Markup.XamlReader]::Parse($xaml)
$window.Topmost = $True  
$window.ShowDialog() #cause Win32Exception

Workaround

$async = $window.Dispatcher.InvokeAsync({
    $window.ShowDialog() | Out-Null
})
$async.Wait() | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment