Home Dashboard Directory Help
Search

Ctrl-C in Read-Host cancels catch statements, hangs debugger by Tim Burris


Status: 

Active


1
0
Sign in
to vote
Type: Bug
ID: 778091
Opened: 1/31/2013 2:29:27 PM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

If the user presses Ctrl-C while Read-Host is awaiting input, I am seeing very strange behavior. While my finally{} blocks get called, catch {} blocks are skipped entirely, even if I throw from one of the finally blocks.

If I set a breakpoint and try to examine variables during this state, the console hangs.

The rest of my script captures Ctrl-C using [Console]::TreatControlCAsInput and [Console]::ReadKey and passes that information up the stack so that the script run can be marked as 'cancelled'. If the user cancels in Read-Host, the run gets marked incorrectly as 'success'.

How can I properly detect that the user cancelled during Read-Host?
Details
Sign in to post a comment.
Posted by Tim Burris on 5/24/2013 at 6:29 PM
I was able to work around "If the user cancels in Read-Host, the run gets marked incorrectly as 'success'" using a structure like this:

$err = "Incomplete"
try { Do-Stuff; $err = $null }
catch { $err = $_ }
finally { if ($err) { Do-ErrorStuff } else { Do-SuccessStuff } }
Posted by Tim Burris on 2/4/2013 at 3:07 PM
PromptForChoices has the same behavior.
Posted by Tim Burris on 1/31/2013 at 2:34 PM
Note: the unexpected behavior in Read-Host persists regardless of the state of [Console]::TreatControlCAsInput.
Sign in to post a workaround.