If I understood your question correctly, you want to exit the application when it's running a task.
If this is the case, you have to separate the task from the thread running the user interface.
One easy way is to utilize
BackgroundWorker[
^]. With that, you can run the task separately and still be able to register button press in the UI. When a cancel button is pressed, simply stop the backrground worker and exit the app.