Application.ApplicationExit 事件

在应用程序即将关闭时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Shared Event ApplicationExit As EventHandler
用法
Dim handler As EventHandler

AddHandler Application.ApplicationExit, handler
public static event EventHandler ApplicationExit
public:
static event EventHandler^ ApplicationExit {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public static void add_ApplicationExit (EventHandler value)

/** @event */
public static void remove_ApplicationExit (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

必须将事件处理程序附加到 Exit 事件,以便在应用程序停止运行前执行未处理的、必需的任务。您可以关闭由该应用程序打开的文件,或释放垃圾回收未回收的对象。

因为这是一个静态事件,所以您必须在 ApplicationExit 事件处理程序本身中分离附加到此事件的任何事件处理程序。如果不分离这些处理程序,它们仍将附加在该事件上并继续占用内存。

示例

下面的代码示例显示两个窗体,并在两个窗体都关闭时退出应用程序。在应用程序启动和退出时,它会记住每个窗体的位置。此示例演示如何使用 ApplicationExit 事件确定何时应该将窗体位置保存到文件,以及何时应该关闭 FileStream

MyApplicationContextApplicationContext 继承,并跟踪每个窗体关闭的时间,然后在两个窗体全部关闭时退出当前线程。该类会记住每个窗体关闭时的位置。当 ApplicationExit 事件发生时,该类将用户的每个窗体的位置写入文件。窗体位置数据存储在标题为 appdata.txt 的文件中,该文件在 UserAppDataPath 确定的位置中创建。Main 方法调用 Application.Run(context) 以启动给定了 ApplicationContext 的应用程序。

这段代码摘录自 ApplicationContext 类概述中显示的示例。有关全部代码的列表,请参见 ApplicationContext

Public Sub New()
    MyBase.New()
    formCount = 0

    ' Handle the ApplicationExit event to know when the application is exiting.
    AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

    Try
        ' Create a file that the application will store user specific data in.
        userData = New FileStream(Application.UserAppDataPath + "\appdata.txt", FileMode.OpenOrCreate)

    Catch e As IOException
        ' Inform the user that an error occurred.
        MessageBox.Show("An error occurred while attempting to show the application." + _
                        "The error is:" + e.ToString())

        ' Exit the current thread instead of showing the windows.
        ExitThread()
    End Try

    ' Create both application forms and handle the Closed event
    ' to know when both forms are closed.
    form1 = New AppForm1()
    AddHandler form1.Closed, AddressOf OnFormClosed
    AddHandler form1.Closing, AddressOf OnFormClosing
    formCount = formCount + 1

    form2 = New AppForm2()
    AddHandler form2.Closed, AddressOf OnFormClosed
    AddHandler form2.Closing, AddressOf OnFormClosing
    formCount = formCount + 1

    ' Get the form positions based upon the user specific data.
    If (ReadFormDataFromFile()) Then
        ' If the data was read from the file, set the form
        ' positions manually.
        form1.StartPosition = FormStartPosition.Manual
        form2.StartPosition = FormStartPosition.Manual

        form1.Bounds = form1Position
        form2.Bounds = form2Position
    End If

    ' Show both forms.
    form1.Show()
    form2.Show()
End Sub

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    ' When the application is exiting, write the application data to the
    ' user file and close it.
    WriteFormDataToFile()

    Try
        ' Ignore any errors that might occur while closing the file handle.
        userData.Close()
    Catch
    End Try
End Sub
private MyApplicationContext() {
    formCount = 0;

    // Handle the ApplicationExit event to know when the application is exiting.
    Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

    try {
        // Create a file that the application will store user specific data in.
        userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate);

    } catch(IOException e) {
        // Inform the user that an error occurred.
        MessageBox.Show("An error occurred while attempting to show the application." + 
                        "The error is:" + e.ToString());

        // Exit the current thread instead of showing the windows.
        ExitThread();
    }

    // Create both application forms and handle the Closed event
    // to know when both forms are closed.
    form1 = new AppForm1();
    form1.Closed += new EventHandler(OnFormClosed);            
    form1.Closing += new CancelEventHandler(OnFormClosing);            
    formCount++;

    form2 = new AppForm2();
    form2.Closed += new EventHandler(OnFormClosed);            
    form2.Closing += new CancelEventHandler(OnFormClosing);            
    formCount++;

    // Get the form positions based upon the user specific data.
    if (ReadFormDataFromFile()) {
        // If the data was read from the file, set the form
        // positions manually.
        form1.StartPosition = FormStartPosition.Manual;
        form2.StartPosition = FormStartPosition.Manual;
        
        form1.Bounds = form1Position;
        form2.Bounds = form2Position;
    }

    // Show both forms.
    form1.Show();
    form2.Show();
}

private void OnApplicationExit(object sender, EventArgs e) {
    // When the application is exiting, write the application data to the
    // user file and close it.
    WriteFormDataToFile();

    try {
        // Ignore any errors that might occur while closing the file handle.
        userData.Close();
    } catch {}
}
   MyApplicationContext()
   {
      formCount = 0;
      
      // Handle the ApplicationExit event to know when the application is exiting.
      Application::ApplicationExit += gcnew EventHandler( this, &MyApplicationContext::OnApplicationExit );
      try
      {
         
         // Create a file that the application will store user specific data in.
         userData = gcnew FileStream( String::Concat( Application::UserAppDataPath, "\\appdata.txt" ),FileMode::OpenOrCreate );
      }
      catch ( IOException^ e ) 
      {
         
         // Inform the user that an error occurred.
         MessageBox::Show( "An error occurred while attempting to show the application. The error is: {0}", dynamic_cast<String^>(e) );
         
         // Exit the current thread instead of showing the windows.
         ExitThread();
      }

      
      // Create both application forms and handle the Closed event
      // to know when both forms are closed.
      form1 = gcnew AppForm1;
      form1->Closed += gcnew EventHandler( this, &MyApplicationContext::OnFormClosed );
      form1->Closing += gcnew CancelEventHandler( this, &MyApplicationContext::OnFormClosing );
      formCount++;
      form2 = gcnew AppForm2;
      form2->Closed += gcnew EventHandler( this, &MyApplicationContext::OnFormClosed );
      form2->Closing += gcnew CancelEventHandler( this, &MyApplicationContext::OnFormClosing );
      formCount++;
      
      // Get the form positions based upon the user specific data.
      if ( ReadFormDataFromFile() )
      {
         
         // If the data was read from the file, set the form
         // positions manually.
         form1->StartPosition = FormStartPosition::Manual;
         form2->StartPosition = FormStartPosition::Manual;
         form1->Bounds = form1Position;
         form2->Bounds = form2Position;
      }

      
      // Show both forms.
      form1->Show();
      form2->Show();
   }

   void OnApplicationExit( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      
      // When the application is exiting, write the application data to the
      // user file and close it.
      WriteFormDataToFile();
      try
      {
         
         // Ignore any errors that might occur while closing the file handle.
         userData->Close();
      }
      catch ( Exception^ ) 
      {
      }

   }


private:
private MyApplicationContext()
{
    formCount = 0;

    // Handle the ApplicationExit event to know 
    // when the application is exiting.
    Application.add_ApplicationExit(new EventHandler(
        this.OnApplicationExit));
    try {
        // Create a file that the application will store 
        // user specific data in.
        userData = new FileStream(Application.get_UserAppDataPath() 
            + "\\appdata.txt", FileMode.OpenOrCreate);
    }
    catch (IOException e) {
        // Inform the user that an error occurred.
        MessageBox.Show("An error occurred while attempting to show the "
            + " application. The error is:" + e.ToString());

        // Exit the current thread instead of showing the windows.
        ExitThread();
    }

    // Create both application forms and handle the Closed event
    // to know when both forms are closed.
    form1 = new AppForm1();
    form1.add_Closed(new EventHandler(OnFormClosed));
    form1.add_Closing(new CancelEventHandler(OnFormClosing));
    formCount++;
    form2 = new AppForm2();
    form2.add_Closed(new EventHandler(OnFormClosed));
    form2.add_Closing(new CancelEventHandler(OnFormClosing));
    formCount++;

    // Get the form positions based upon the user specific data.
    if (ReadFormDataFromFile()) {
        // If the data was read from the file, set the form
        // positions manually.
        form1.set_StartPosition(FormStartPosition.Manual);
        form2.set_StartPosition(FormStartPosition.Manual);
        form1.set_Bounds(form1Position);
        form2.set_Bounds(form2Position);
    }

    // Show both forms.
    form1.Show();
    form2.Show();
} //MyApplicationContext

private void OnApplicationExit(Object sender, EventArgs e)
{
    // When the application is exiting, write the application data to the
    // user file and close it.
    WriteFormDataToFile();
    try {
        // Ignore any errors that might occur while closing the file handle.
        userData.Close();
    }
    catch (System.Exception exp) {
    }
} //OnApplicationExit

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Application 类
Application 成员
System.Windows.Forms 命名空间
Exit