Application.Exit 方法

定義

通知所有必須結束的訊息幫浦,接著在訊息處理完成之後關閉所有應用程式視窗。

多載

Exit()

通知所有必須結束的訊息幫浦,接著在訊息處理完成之後關閉所有應用程式視窗。

Exit(CancelEventArgs)

通知所有必須結束的訊息幫浦,接著在訊息處理完成之後關閉所有應用程式視窗。

Exit()

通知所有必須結束的訊息幫浦,接著在訊息處理完成之後關閉所有應用程式視窗。

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

範例

下列程式碼範例會列出表單清單方塊中的數位。 每次按一下 button1 時,應用程式都會將另一個數位新增至清單。

方法 Main 會呼叫 Run 以啟動應用程式,這會建立表單 、 listBox1button1 。 當使用者按一下 button1 時,方法會將 button1_Click 第一個數位新增至清單方塊,並顯示 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 的 屬性設定 Canceltrue ,藉此取消此事件。

  • 如果其中一個處理常式取消事件,則會 Exit 傳回而不進一步動作。 否則, FormClosed 會針對每個開啟的表單引發事件,然後關閉所有執行中的訊息迴圈和表單。

注意

方法 Exit 不會引發 ClosedClosing 事件,自 .NET Framework 2.0 起已過時。

另請參閱

適用於

Exit(CancelEventArgs)

通知所有必須結束的訊息幫浦,接著在訊息處理完成之後關閉所有應用程式視窗。

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 取消結束。

另請參閱

適用於