0

I am working on ASP.net to open a file for edit till the session ends for various reasons exclusively. When user Logout from the session I close the opened file (from the Session variable) so that other users can access it. It works fine as long as user Signout manually or Session time expires. The issue is, when user close the browser without "Signout" link, the Session_End function not being called from Global.asax file. There may be other scenarios like, Net disconnect, browser crash, System shutdown etc.

Here is my code in Global.asax file

  Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
       ' Code the close the File which is being stored in Session
     End Sub

I am looking either of these solutions

  • How to call the "Session_End" function when user close the browser.
  • Or how to access the "unclosed" Session variables later on to free them.

2 Answers 2

0

When you work with files on a web service (or with any other unmanaged resources), it's best not to keep them open. There is no way you can handle some of the disconnections mentioned by you in the question.

It's best to open the file, do the job you need and close it immediately without keeping it open. Next time you have to access it again, open, do the job and close.

1
  • In my scenario, I need to lock something if one user is working on it and enable other users once they logout or close the browser.
    – AjayR
    Nov 20, 2013 at 4:34
0

There is unfortunately no way to trigger Session_OnEnd when the user closes their browser; simply because ASP.NET is unaware that this has happened, and relies on the session timing out (which it will do some time after, the default being 20 minutes later).

I think in your scenario you might want to use JavaScript to alert/prevent the user from closing the current window. Try this (from another StackOverflow answer): -

javascript detect browser close tab/close browser

Hope that helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.