次の方法で共有


Application.Exit メソッド

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

Public Shared Sub Exit()
[C#]
public static void Exit();
[C++]
public: static void Exit();
[JScript]
public static function Exit();

解説

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

注意    Application.Exit メソッドが呼び出されてアプリケーションを終了する場合、 Form.Closed イベントと Form.Closing イベントは発生しません。これらのイベントのいずれかに実行する必要がある検証コードがある場合は、 Exit メソッドを呼び出す前に、開いている各フォームに対して Form.Close メソッドを個別に呼び出す必要があります。

使用例

[Visual Basic, C#, C++] フォームのリスト ボックスに番号を一覧表示する例を次に示します。 button1 をクリックするたびに、リストに新しい番号が追加されます。

[Visual Basic, C#, C++] Main メソッドは、 Run を呼び出してアプリケーションを開始し、フォーム、 listBox1 、および button1 を作成します。 button1 をクリックすると、 button1_Click メソッドによってリスト ボックスに 1 から 3 の番号が追加され、 MessageBox が表示されます。 MessageBox の [ いいえ] をクリックすると、 button1_Click メソッドによってリストに新しい番号が追加されます。[ はい] をクリックすると、アプリケーションでは Exit を呼び出してキューに残っているメッセージをすべて処理し、終了します。

[Visual Basic, C#, C++] この例は、 listBox1button1 が既にインスタンス化され、フォームに配置されていることを前提としています。

 
<STAThread()> _
Shared Sub Main()     
   ' Starts the application.
   Application.Run(New Form1())
End Sub

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


[C#] 
public static void Main(string[] args) {
    // Starts the application.
    Application.Run(new Form1());
 }
 
 protected 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();
 }


[C++] 
public:
   static void main() {
      // Starts the application.
      Application::Run(new Form1());
   }

protected:
   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(__box(j));
      }

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

         // The user wants to exit the application. Close everything down.
         Application::Exit();
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

.NET Framework セキュリティ:

参照

Application クラス | Application メンバ | System.Windows.Forms 名前空間 | ExitThread | Run