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

Definition

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Overloads

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 ()

Examples

The following code example lists numbers in a list box on a form. Each time you click button1, the application adds another number to the list.

The Main method calls Run to start the application, which creates the form, listBox1, and button1. When the user clicks button1, the button1_Click method adds numbers one to three to the list box, and displays a MessageBox. If the user clicks No on the MessageBox, the button1_Click method adds another number to the list. If the user clicks Yes, the application calls Exit, to process all remaining messages in the queue and then to quit.

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

Remarks

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. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

Exit raises the following events and performs the associated conditional actions:

  • A FormClosing event is raised for every form represented by the OpenForms property. This event can be canceled by setting the Cancel property of their FormClosingEventArgs parameter to true.

  • If one of more of the handlers cancels the event, then Exit returns without further action. Otherwise, a FormClosed event is raised for every open form, then all running message loops and forms are closed.

Note

The Exit method does not raise the Closed and Closing events, which are obsolete as of .NET Framework 2.0.

Security

SecurityPermission
for the immediate caller to call unmanaged code. Associated enumeration: UnmanagedCode

See Also

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)
Parameters
e
CancelEventArgs CancelEventArgs CancelEventArgs CancelEventArgs

Returns whether any Form within the application cancelled the exit.

Security

SecurityException
For permission to terminate a running application thread. Associated enumeration: UnmanagedCode

See Also

Applies to