Click here to Skip to main content
15,887,364 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C#
I have asp.net application. I am forcibly end this application and on the time of that i want to update my view page count value in database. If anyone known plz help me.
I also written code in global.asax file
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
but this is not working when i forcibly end application. 
Posted
Comments
Suvendu Shekhar Giri 2-Dec-15 0:32am    
Are you getting any error?
What was your observation during debugging?
Member 10711621 2-Dec-15 0:41am    
there is no error, its working but. i want to update page view counter data when application forcibly end and Application_Event will fired when application gracefully ends. i want to update data in both cases either application gracefully end or forcibly end
[no name] 2-Dec-15 1:47am    
forcibly end application means what? You are closing the browser or end the process in Task manager?
Member 10711621 2-Dec-15 1:57am    
I am ending the process in Task manager and shut down system too while my application is running.
FrancoisViljoen 2-Dec-15 4:41am    
I think if you shut down the system in Task Manager then no more code will execute so you will see the effect that you are seeing. I would recommend storing a global variable with view count and updating the value in the database periodically (when the App Server is quiet). then if the service crashes (or gets killed) then you will at least have some indication of page count (albeit not 100% correct). Without getting too complex, this is probably what I would do.


1 solution

If you're talking about a web application loaded into a standard web browser then you're going to struggle to do this consistently.

When the browser loads a new page, closes a tab or the whole window closes, it will try and call the following events executing that action:

onBeforeUnload
onUnload

You can write code on the client side to call an AJAX request when these events fire.

We use this mechanism to implement pessimistic record locking.

But it does not work every time. Some times the browser just doesn't wait long enough for the action to complete before the page closes.

Doing End Task the control pan

We've also had to have a timer running on the servers to remove record locks not renewed in the last 5 minutes.

We've also put code to clear up locks on the global session end event on the server.
 
Share this answer
 
Comments
Member 10711621 2-Dec-15 5:44am    
i want to update my page count data once in 24 hrs because my application is running Lac's of systems in one time
Stephen Hewison 2-Dec-15 5:46am    
Then use the global begin request method. This is called for every page request. If you want a distinct user count the use the session start event.
Member 10711621 2-Dec-15 5:51am    
void Application_End(object sender, EventArgs e)
{
BannerHelper bannerHelper;
if (((DefaultPageClass)Application["DefaultPageData"]).BannersViewsApplication != null)
{
//Update the Banner's views in the database.
bannerHelper = new BannerHelper();
foreach (DictionaryEntry DE in ((DefaultPageClass)Application["DefaultPageData"]).BannersViewsApplication)
{
bannerHelper.UpdatePageViews(int.Parse(DE.Key.ToString()), int.Parse(DE.Value.ToString()));
}
}
/*LinkAd Views */
if (((DefaultPageClass)Application["DefaultPageData"]).LinkAdViews != null)
{
//Update the Banner's views in the database.
bannerHelper = new BannerHelper();
foreach (DictionaryEntry DE in ((DefaultPageClass)Application["DefaultPageData"]).LinkAdViews)
{
bannerHelper.UpdateLinkAdViews(int.Parse(DE.Key.ToString()), int.Parse(DE.Value.ToString()));
}
}
}
Member 10711621 2-Dec-15 5:55am    
this code is not executing while process kill forcibly. that's problem
Stephen Hewison 2-Dec-15 6:39am    
What process? The IIS worker process? Application_End is called when a work process is recycled or the application pool is stopped e.t.c.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900