Solved

Application_Start, Application_End & Session_End

Posted on 2006-11-20
5
1,439 Views
Last Modified: 2012-05-05
I have a few things to confirm on these three events in the global.asax file

Application_Start - fires when the first user accessses the application.
Applicaton_end - when the last user's session's times out
Session_End - when the users's session times out.

Does closing the browser invoke the Session_End?
If the last user closes the browser does it invoke Application_End?

I have a web application where I need to clean up some temp files in a particular directory, I need to do that when the session ends or when the application ends.
The users access this website from 9 -5, so will the application end event always be invoked if there is no activity past the last session timeout.?

Will the application start event fire the next day when the users hit the website again? please note that I don't intend to start and stop IIS everyday.
Thanks
0
Comment
Question by:countrymeister
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17979878
>>Applicaton_end - when the last user's session's times out => No: Application_End event is fired when the web site is stopped or IIS is reset

>>Does closing the browser invoke the Session_End? => No
>>If the last user closes the browser does it invoke Application_End? => No

>>I have a web application where I need to clean up some temp files in a particular directory, I need to do that when the session ends or when the application ends.
>>The users access this website from 9 -5, so will the application end event always be invoked if there is no activity past the last session timeout.?

I recommend you this using an ASPX page and call this page using Windows Task Schedular once a day at a specific time. Another option would be to place code in Session_Start event to delete files that are older than one day.


>>Will the application start event fire the next day when the users hit the website again? please note that I don't intend to start and stop IIS everyday => No

HTH, Nauman.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 17979987
Thanks, so Session_End will only be invoked when the user's session time's out, meaning he is logged on for the timeout period with no activity.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17980336
Yes, Session_End is only invoked when the session is timed out or you call Session.Abandon from your code.

--Nauman.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 17980725
Nauman,

Thanks for your help

This is what I came up with in my Session_Start and Session_End. In Session_Start i delete all files that are not equal to current date
In Session_End i delete user specific session file.
I tested this, and you are right that when I close the browser the session_end is not invoked as my file is not deleted.

       // Code that runs when a new session is started
        try
        {
            FileSystemObject fso = new FileSystemObject();
            string path = Server.MapPath(".");
            path = path + "\\PDFFiles";
            if (fso.FolderExists(path))
            {
                //delete each file in the folder, do not delete the folder as this would remove the folder privileges
                DirectoryInfo dirInfo = new DirectoryInfo(path);

                foreach (FileInfo f in dirInfo.GetFiles())
                {
                    if ((f.Extension.Equals(".pdf")) && ((f.CreationTime.ToShortDateString()) != (DateTime.Now.ToShortDateString())))
                    {
                       
                        f.Delete();
                    }

                }
            }


        }

        // 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.

        try
        {
            FileSystemObject fso = new FileSystemObject();
            string strFile = Session.SessionID.ToString() + ".pdf";
            string path = Server.MapPath(".");
            path = path + "\\PDFFiles";
            string pdf = path + "\\" + strFile;
            fso.GetFile(pdf);
            fso.DeleteFile(pdf, true);


        }
        catch (Exception ex)
        { }

0
 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 250 total points
ID: 17980844
I think the top portion of the code will be able to handle the deletion. There will be no need for the code in session_End event.

-Nauman.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now