次の方法で共有


Application.Exit メソッド

定義

終了する必要があることをすべてのメッセージ ポンプに通知し、メッセージの処理後にすべてのアプリケーション ウィンドウを閉じます。

オーバーロード

名前 説明
Exit()

終了する必要があることをすべてのメッセージ ポンプに通知し、メッセージの処理後にすべてのアプリケーション ウィンドウを閉じます。

Exit(CancelEventArgs)

終了する必要があることをすべてのメッセージ ポンプに通知し、メッセージの処理後にすべてのアプリケーション ウィンドウを閉じます。

Exit()

ソース:
Application.cs
ソース:
Application.cs
ソース:
Application.cs
ソース:
Application.cs
ソース:
Application.cs

終了する必要があることをすべてのメッセージ ポンプに通知し、メッセージの処理後にすべてのアプリケーション ウィンドウを閉じます。

public:
 static void Exit();
public static void Exit();
static member Exit : unit -> unit
Public Shared Sub Exit ()

次のコード例では、フォームのリスト ボックスに数値を一覧表示します。 button1をクリックするたびに、アプリケーションはリストに別の番号を追加します。

Main メソッドはRunを呼び出してアプリケーションを起動し、フォーム、listBox1、およびbutton1を作成します。 ユーザーが button1をクリックすると、 button1_Click メソッドはリスト ボックスに数値 1 から 3 を追加し、 MessageBoxを表示します。 ユーザーがMessageBox[いいえ] をクリックすると、button1_Click メソッドによってリストに別の番号が追加されます。 ユーザーが [ はい] をクリックすると、アプリケーションは Exitを呼び出して、キュー内の残りのすべてのメッセージを処理してから終了します。

この例では、 listBox1button1 がインスタンス化され、フォームに配置されている必要があります。

public:
   static void main()
   {
      // Starts the application.
      Application::Run( gcnew Form1 );
   }

private:
   void button1_Click( Object^ sender, System::EventArgs^ e )
   {
      // Populates a list box with three numbers.
      int i = 3;
      for ( int j = 1; j <= i; j++ )
      {
         listBox1->Items->Add( j );
      }
      
      /* Determines whether the user wants to exit the application.
       * If not, adds another number to the list box. */
      while ( MessageBox::Show( "Exit application?", "",
         MessageBoxButtons::YesNo ) == ::DialogResult::No )
      {
         // Increments the counter ands add the number to the list box.
         i++;
         listBox1->Items->Add( i );
      }
      
      // The user wants to exit the application. Close everything down.
      Application::Exit();
   }
public static void Main(string[] args) {
    // Starts the application.
    Application.Run(new Form1());
 }

 private void button1_Click(object sender, System.EventArgs e) {
    // Populates a list box with three numbers.
    int i = 3;
    for(int j=1; j<=i; j++) {
       listBox1.Items.Add(j);
    }

    /* Determines whether the user wants to exit the application.
     * If not, adds another number to the list box. */
    while (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) ==
       DialogResult.No) {
       // Increments the counter ands add the number to the list box.
       i++;
       listBox1.Items.Add(i);
    }

    // The user wants to exit the application. Close everything down.
    Application.Exit();
 }
<STAThread()> _
Shared Sub Main() 	
   ' Starts the application.
   Application.Run(New Form1())
End Sub

Private Sub button1_Click(sender As object, e As System.EventArgs)
   ' Populates a list box with three numbers.
   Dim i As Integer = 3
   Dim j As Integer
   For j = 1 To i - 1
      listBox1.Items.Add(j)
   Next

   ' Checks to see whether the user wants to exit the application.
   ' If not, adds another number to the list box.
   While (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) = _ 
      DialogResult.No)
      ' Increments the counter and adds the number to the list box.
      i = i + 1
      listBox1.Items.Add(i)
   End While

   ' The user wants to exit the application. Close everything down.
   Application.Exit()
End Sub

注釈

Exit メソッドは、すべてのスレッドで実行中のすべてのメッセージ ループを停止し、アプリケーションのすべてのウィンドウを閉じます。 このメソッドは、必ずしもアプリケーションを強制的に終了させるわけではありません。 Exit メソッドは通常、メッセージ ループ内から呼び出され、Runが強制的に返されます。 現在のスレッドのメッセージ ループのみを終了するには、 ExitThreadを呼び出します。

Exit は、次のイベントを発生させ、関連する条件付きアクションを実行します。

  • FormClosing イベントは、OpenForms プロパティによって表されるすべてのフォームに対して発生します。 このイベントは、FormClosingEventArgs パラメーターの Cancel プロパティを true に設定することで取り消すことができます。

  • いずれかのハンドラーがイベントを取り消した場合、 Exit はそれ以上の操作を行わずに戻ります。 それ以外の場合は、開いているすべてのフォームに対して FormClosed イベントが発生し、実行中のすべてのメッセージ ループとフォームが閉じられます。

Exit メソッドでは、ClosedイベントとClosingイベントは発生しません。これは古い形式です。

こちらもご覧ください

適用対象

Exit(CancelEventArgs)

ソース:
Application.cs
ソース:
Application.cs
ソース:
Application.cs
ソース:
Application.cs
ソース:
Application.cs

終了する必要があることをすべてのメッセージ ポンプに通知し、メッセージの処理後にすべてのアプリケーション ウィンドウを閉じます。

public:
 static void Exit(System::ComponentModel::CancelEventArgs ^ e);
public static void Exit(System.ComponentModel.CancelEventArgs e);
public static void Exit(System.ComponentModel.CancelEventArgs? e);
static member Exit : System.ComponentModel.CancelEventArgs -> unit
Public Shared Sub Exit (e As CancelEventArgs)

パラメーター

e
CancelEventArgs

アプリケーション内の Form が終了を取り消したかどうかを返します。

こちらもご覧ください

適用対象