Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
マイクロソフトの田中達彦です。
Windows API Code Packの記事の第2回目、第3回目にあるアプリケーションの再起動と修復について質問を受けました。
「アプリケーションが予期せずクラッシュしたときにデータを退避させて復活させる機能は役に立つのだが、クラッシュのさせかたが分からないのでテストできない」
おっしゃるとおりです。
せっかく実装しても、クラッシュに遭遇しなければ、実装したコードがちゃんと動くかどうか分かりません。
そこで、強制的にクラッシュさせる方法を紹介します。
強制的にクラッシュさせるには、以下の1文を実行するだけです。
Environment.FailFast("強制終了");
引数の文字列としてここでは "強制終了" という文字列を入れていますが、何でも構いません。
使用方法としては、例えばボタンを1つ貼り付けておいて、そのボタンをクリックしたら上記のコードを実行するという方法があります。
アプリケーションの再起動と修復の記事のときにも書いているように、以下の点に注意してください。
- Visual Studioからデバッグ付きで実行してしまうと、アプリケーションのクラッシュをVisual Studioが拾ってしまうので、デバッグなしで実行してください。
- アプリケーションの起動から60秒間は、再起動と修復の機能を利用できません。
マイクロソフト
田中達彦
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in