Posted by: Cirilo Meggiolaro | 03/12/2009

Tip of the day #149 – If you have started, finish it!

Let’s talk today about two important events from the AppDomains that you may need to take care about: DomainUnload and ProcessExit.

DomainUnload Event

The DomainUnload event is raised when an AppDomain is about to unload. You may use this event to perform finalization activities before the application domain unloads. It’s important to note that this event is not raised on the default application domain and you cannot predict the thread that is going to handle the event.

To register an event handler for this event:

AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_DomainUnload);

Your event handler will be similar to the following:

private void CurrentDomain_DomainUnload(object sender, EventArgs e) { }

ProcessExit Event

Different from the DomainUnload event, the ProcessExit event is raised when the parent’s process exits. Use it to perform tasks such as close files and connections to a database.

You may add event handlers for each application domain but be careful because there is a time limit of 3 seconds for the execution of all ProcessExit event handlers.

To register an event handler for this event:

AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);

Your event handler will be similar to the following:

private void CurrentDomain_ProcessExit(object sender, EventArgs e) { }

It’s healthy and responsible that you finalize and release your resources properly and execute any pending task. Keep this in mind and improve your application’s code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: