Ignore CTRL+C in delay - powershell

512 views Asked by At

Whenever I set a delay in powershell using Start-Sleep, e.g:

Start-Sleep 10

then it does not ignore CTRLC. I mean when I hit that key stroke the delay quits. How can I ignore it in delays?

2

There are 2 answers

0
mklement0 On BEST ANSWER

You can temporarily set [Console]::TreatControlCAsInput to $true:

[Console]::TreatControlCAsInput = $true
Start-Sleep 10  # Ctrl-C will now not abort this sleep.
[Console]::TreatControlCAsInput = $false
0
Wasif On

I have found a way to do this:

[System.Threading.Thread]::Sleep(milliseconds)

This ignores CTRL-C on delays.