共用方式為


Application.Exit 方法

定義

通知所有訊息泵必須終止,並在訊息處理後關閉所有應用程式視窗。

多載

名稱 Description
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,應用程式都會在列表中新增一個數字。

方法呼叫MainRun啟動應用程式,產生表單、 listBox1button1。 當使用者點擊 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 引發以下事件並執行相關的條件動作:

  • 每個由OpenForms該屬性所代表的形式都會引發一個FormClosing事件。 此事件可透過將參數FormClosingEventArgstrue屬性設Cancel為 來取消。

  • 若多位處理者取消事件,則 Exit 返回不做任何行動。 否則, FormClosed 每個開啟的表單都會觸發事件,然後所有正在執行的訊息迴圈和表單都會被封閉。

備註

Exit 方法不會提升 ClosedClosing 事件,而事件已過時。

另請參閱

適用於

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 人取消了退出。

另請參閱

適用於