Application.ApplicationExit Evento
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Si verifica quando l'applicazione è in fase di chiusura.
public:
static event EventHandler ^ ApplicationExit;
public static event EventHandler ApplicationExit;
member this.ApplicationExit : EventHandler
Public Shared Custom Event ApplicationExit As EventHandler
Tipo evento
Esempio
Nell'esempio di codice seguente vengono visualizzati due moduli e l'applicazione viene chiusa quando entrambi i moduli vengono chiusi. All'avvio e all'uscita dell'applicazione, viene memorizzata la posizione di ogni modulo. In questo esempio viene illustrato l'utilizzo dell'evento ApplicationExit
per sapere quando le posizioni del modulo devono essere mantenute nel file e quando FileStream
deve essere chiuso.
La classe MyApplicationContext
eredita da ApplicationContext e tiene traccia di quando ogni maschera viene chiusa e chiude il thread corrente quando sono entrambi. La classe memorizza la posizione di ogni modulo quando viene chiusa. Quando si verifica l'evento ApplicationExit
, la classe scrive le posizioni di ogni utente nel file. I dati sulla posizione del modulo vengono archiviati in un file denominato appdata.txt
creato nel percorso determinato da UserAppDataPath. Il Main
metodo chiama Application.Run(context)
per avviare l'applicazione in base a ApplicationContext.
Questo codice è un estratto dell'esempio illustrato nella panoramica della ApplicationContext classe. Vedere ApplicationContext per l'intero elenco di codice.
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.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 { }
}
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
Commenti
È necessario collegare i gestori eventi all'evento ApplicationExit
per eseguire attività non gestite e necessarie prima che l'applicazione venga arrestata. È possibile chiudere i file aperti dall'applicazione o eliminare gli oggetti non recuperati da Garbage Collection.
Poiché si tratta di un evento statico, è necessario scollegare tutti i gestori eventi associati a questo evento nel ApplicationExit
gestore eventi stesso. Se questi gestori non vengono scollegati, rimarranno collegati all'evento e continueranno a utilizzare la memoria.