Catch the close of the application to save info

D

Dan Aldean

Hi,

I have an application that needs to save some info in the database when the
user closes the application by clicking the x box in the right top corner of
the window.

I tried the session_end event in the global module but it only fires with
the timeout, not when the app is closed.
Application_end doesn't work either.

I use InProc in web.config.

Thanks
 
M

Mark Rae

I have an application that needs to save some info in the database when
the user closes the application by clicking the x box in the right top
corner of the window.

Ah yes - this one comes up fairly regularly...
I tried the session_end event in the global module but it only fires with
the timeout, not when the app is closed.

That's right - the server has no knowledge whatever of what is happening in
the browser until / unless the browser sends it another HttpRequest, due to
the disconnected architecture of the web. This is a common misconception...
Application_end doesn't work either.

Indeed not - Application_End fires when the final live session is torn down,
either through code or through automatic timeout.
I use InProc in web.config.

Irrelevant...

Your only option is to try to use the client-side window.onunload event:
http://www.google.co.uk/search?sour...G:2006-28,GGLG:en&q="ASP.NET"+"window.unload"

However, be aware that this is not guaranteed to work the same way for all
browsers across all platforms, and will almost certainly also fire if the
user refreshes the page...
 
L

Laurent Bugnion [MVP]

Hi,

Dan said:
Hi,

I have an application that needs to save some info in the database when the
user closes the application by clicking the x box in the right top corner of
the window.

I tried the session_end event in the global module but it only fires with
the timeout, not when the app is closed.
Application_end doesn't work either.

I use InProc in web.config.

Thanks

There is no way to detect with certainty when the browser window is closed.

If it's really vital for you to save the information in the DB as soon
as possible, the only way I can think of is having a web service
periodically "ping" the server. When the "ping" stops, it means that the
user closed the window, or that he navigated to another page.

However, the normal way to do things is to use Session_End. Why can't
you wait until Session_End?

HTH,
Laurent
 
J

John Timney \(MVP\)

Session timeout is the correct place to do this, so work your strategy
around that and ignore whats happening at the client. Your server code
knows nothing about the browser being closed, or even if it has a window at
all and isn't a simple tcp socket call over http from a webservice.

--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top