0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#でWindowsFormアプリケーションを強制終了する

Last updated at Posted at 2023-07-18

1. はじめに

  • WindowsForm内で致命的なエラーが発生した場合にアプリケーションを強制終了させたい

2. 開発環境

  • C#
  • .Net 6
  • Visual Studio 2022
  • Windows 11

3. ソースコード

3.1. WindowsFormアプリケーションを強制終了する

  • メソッドの実行途中で強制終了する
private void Form1_Load(object sender, EventArgs e)
{
   // 開始処理
   Debug.WriteLine("Start");

   // アプリケーション強制終了
   Environment.Exit(0x8020);

   // 終了処理 → 呼び出されない
   Debug.WriteLine("End");
}

3.2. WindowsFormアプリケーションを終了する

  • メソッドを実行した後に終了する
private void Form1_Load(object sender, EventArgs e)
{
   // 開始処理
   Debug.WriteLine("Start");

   // アプリケーション終了
   System.Windows.Forms.Application.Exit();

   // 終了処理 → 呼び出されて終了
   Debug.WriteLine("End");
}

4. 参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?