Application.Exit Application.Exit Application.Exit Application.Exit Method

定义

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

重载

Exit() Exit() Exit() Exit()

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs)

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Exit() Exit() Exit() Exit()

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

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

示例

下面的代码示例在窗体上的列表框中列出了数字。The following code example lists numbers in a list box on a form. 每次单击button1时, 应用程序都会向列表中添加一个编号。Each time you click button1, the application adds another number to the list.

方法调用Run以启动应用程序, 该应用程序将创建窗listBox1体、 button1和。 MainThe Main method calls Run to start the application, which creates the form, listBox1, and button1. 用户单击button1时, 该button1_Click方法会向列表MessageBox框中添加一个到三个数字, 并显示。When the user clicks button1, the button1_Click method adds numbers one to three to the list box, and displays a MessageBox. 如果用户单击上的 "否MessageBox", button1_Click则该方法将向列表中添加另一个数字。If the user clicks No on the MessageBox, the button1_Click method adds another number to the list. 如果用户单击 "是", 应用程序Exit将调用来处理队列中所有剩余的消息, 然后退出。If the user clicks Yes, the application calls Exit, to process all remaining messages in the queue and then to quit.

该示例要求listBox1button1已经实例化并将其放置在窗体上。The example requires that listBox1 and button1 have been instantiated and placed on a form.

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方法在所有线程上停止所有正在运行的消息循环, 并关闭应用程序的所有窗口。The Exit method stops all running message loops on all threads and closes all windows of the application. 此方法并不一定强制退出应用程序。This method does not necessarily force the application to exit. 方法通常从消息循环中调用, 并强制Run返回。 ExitThe Exit method is typically called from within a message loop, and forces Run to return. 若要仅退出当前线程的消息循环, 请调用ExitThreadTo exit a message loop for the current thread only, call ExitThread.

Exit引发以下事件并执行关联的条件操作:Exit raises the following events and performs the associated conditional actions:

  • FormClosingOpenForms由属性表示的窗体都将引发一个事件。A FormClosing event is raised for every form represented by the OpenForms property. 可以通过将其Cancel FormClosingEventArgs参数的属性设置为来true取消此事件。This event can be canceled by setting the Cancel property of their FormClosingEventArgs parameter to true.

  • 如果有一个或多个处理程序取消了该事件Exit , 则返回而不执行进一步操作。If one of more of the handlers cancels the event, then Exit returns without further action. 否则, FormClosed会为每个打开的窗体引发事件, 并关闭所有正在运行的消息循环和窗体。Otherwise, a FormClosed event is raised for every open form, then all running message loops and forms are closed.

备注

方法不会Closed引发和Closing事件, 这些事件在中已过时.NET Framework 2.0.NET Framework 2.0ExitThe Exit method does not raise the Closed and Closing events, which are obsolete as of .NET Framework 2.0.NET Framework 2.0.

安全性

SecurityPermission
供直接调用方调用非托管代码。for the immediate caller to call unmanaged code. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode

另请参阅

Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs)

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

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 CancelEventArgs CancelEventArgs CancelEventArgs

返回应用程序中的任何 Form 是否已取消退出。Returns whether any Form within the application cancelled the exit.

安全性

SecurityException
用于终止正在运行的应用程序线程的权限。For permission to terminate a running application thread. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode

另请参阅

适用于