qshinoの日記

Powershell関係と徒然なこと

CTRL-C 処理 by PowerShell

CTRL-C by PowerShell

PowerShellでCTRL-Cを処理する方法

  • Polling
  • finally

finally

CTRL-Cや、exit() 実行時でもfinallyが実行される。

polling

CTRL-Cをキー入力処理するフラグをセットし、以後はCTRL-Cをキー入力としてポーリング。

フラグセット

キーのポーリング

if($Host.UI.RawUI.KeyAvailable -and (3 -eq [int] $Host.UI.RawUI.ReadKey(“AllowCtrlC,IncludeKeyUp,NoEcho”).Character))

参考

http://stackoverflow.com/questions/1710698/gracefully-stopping-in-powershell

http://poshcode.org/542