Detecting when user closes flash player to throw a method

Hello buddies!

My question is pretty simple. There is a way to know when the user closes the flash player?
I’m developing a “desk application” and i want to save the state of it by launching a method when the user exits the app. The method would update/create a XML file with the current state.

I’m pretty newbie with AS so I have no clue if thats possible. By the way, I’m coding with AS3.

Feedback appreciated!!!