Is it possible to detect in Flex application browser window close event so that an action can be
started when user closes Flex application, does anyone know how to do that if it's possible in the
first place? The reason why i am asking this is because i have a multiuser Flex application where
every user has it's own directory on a server side. Application has logout button which triggers
cleanup of user's directory but what if the user just closes the window? I would like to be able
to lunch that same cleanup upon browser close window
In the page hosting your app, write a Javascript function triggered by window.onbeforeunload, and this function can call a function inside your Flex application.
Note that the onbeforeunload function is not guaranteed to work for all browsers.
I would not recommend that approach because the closing action fails too often, meaning worthless. My browser freezes and force-quitted several times a day. My computer sometimes freezes. My internet connection sometimes dies. I think, some browsers even do not guarantee those kinds of actions executed every time.
So, the session timeout might be one safe way in most cases.
You can also try having a socket connection, so that your server can ping if a user is alive and also can detect if socket is closed. Even socket, however, can be unresponsive or can be disconnected sometimes while user is still using the application.
You might want to be strategic.
Related
i run a background thread in Application_Start() in global.asax
to use it like windows service
but applicaton_end fire when all session is ended in my website
i have a sms Business that work with webservice and i want to have agent in my server that
check incoming message like windows service
i increase session timeout to 10 hours but when i close browser application end fired and
my thread stop working
i cant change server properties
can i disable application_end to thread keep working?
I apologize to you because of poor english writing
You should make a separate program that runs as a service or a batch program to do that. Although ASP.NET persists static variables and such between page loads, you shouldn't be using it to run things in the background. There is no guarantee that things will keep running.
If you need something to happen on the user's end when something happens on the server (like a message received (like on facebook), someone responds to a request, or some other event is raised) you need to implement a polling system in javascript on the webpage that the user sees which uses AJAX (look it up...its such an awesome concept) to periodically talk to the server to see if anything happened. That is how Facebook chat works along with most webmail systems to check for new messages. The server doesn't talk to the browser...the browser asks the server if anything happened.
Here is an example of a chat program that uses AJAX: http://www.dynamicajax.com/fr/JSON_AJAX_Web_Chat-.html
It isn't exactly what you are doing, but it demonstrates the concept of trying to check something after the page has been loaded and making the user's browser respond.
Nay be use a .NET version of quartz-scheduler library (http://quartz-scheduler.org/) for defining and triggering a job based on the configuration (cron expresion).
iis automatically close website application when no session is open in website
for disable this you can change idle time in iis or read website link in global.asax (application_end) function to start a new session
I have an aspx page with a simple form to send emails to pre-defined lists of users. On the longer lists the page usually times out before the emails finish sending but this has never been an issue.
Today something weird happened and each user got four emails. In the log I could see three new threads crank up one at a time and start over sending from the beginning of the list.
Any ideas? I absolutely know I didn't intentionally refresh the Web page myself, and certainly not three times. But could the browser (IE8) have done it? Would it post again trying to re-establish a connection when it timed out? Or when I switched back to the browser window from another app? I have never seen behavior like this before.
First question would be whether there is any reason to do a long-running task syncronously, i.e. lock up a thread that should be serving web requests for something that could be done in the background, while the browser sits and waits for a response that its probably not going to get. I'd look into running this asynchronously unless there's a very deliberate reason not to.
Secondly have you looked into creating some kind of locking mechanism such that the process can't be started more than once? I have processes where I add a token to the application cache (and remove it when I'm done) so that if the token exists the process won't run again (the call to the asynch task isn't made), and that does the job. That way it doesn't matter how many clients call your code, you prevent things happening more than they should.
I have a Menu page. If a user selects an Menu Item it opens a new IE Window using JavaScript. So user can open different parts of applications in multiple IE Windows. These Windows have the same Session.
My issue is that these pages are accessed synchronously? If one of the child window is waiting for an action to be finished no other request from any other child window is processed. Is it because of using Session variables?
Update: This is only happening to the windows having the same parent. If I have IE child windows from different parent windows then this issue is not there.
Yes, if each page is using the session, then asp.net will serialize activity against it. If one or more of these sub-pages only need read only access, then mark that in your #Page directives (e.g. EnableSessionState="ReadOnly"), or turn it off completely if the session information isn't used.
Generally, it's a bad plan to have long-running activities pending on the server, and as you've found, this is especially true if they're using the session.
Edit
Last FAQ here also describes this serialization.
Edit 2
In response to comment re: closing the child window:
It will eventually process other requests, once the server side process finishes whatever request it's been working on. Closing a child window does not abort the request on the server side. Best you can hope is that the long-running request has a chance to check IsClientConnected every so often, and aborts its processing if it's no longer relevant.
From the server perspective, you have absolutely no way to know how many browser windows the client has open.
So, no, they are not synchronous.
I am not able to make more than one request at a time in asp.net while the session is active. Why does this limitation exist? Is there a way to work around it?
This issue can be demonstrated with a WebForms app with just 3 simple aspx pages (although the limitation still applies in asp.net mvc).
Create an asp.net 3.5 web application.
There should be just three pages:
NoWait.aspx, Wait.aspx, and SessionStart.aspx
NoWait.aspx has this single nugget added between the default div tags: <%=DateTime.Now.Ticks %>. The code-behind for this page is the default (empty).
Wait.aspx looks just like NoWait.aspx, but it has one line added to Page_Load in the code-behind: Thread.Sleep(3000); //wait 3 seconds
SessionStart.aspx also looks just like NoWait.aspx, but it has this single line in its code-behind: Session["Whatever"] = "Anything";
Open a browser and go to NoWait.aspx. It properly shows a number in the response, such as: "633937963004391610". Keep refreshing and it keeps changing the number. Great so far! Create a new tab in the same browser and go to Wait.aspx. It sits for 3 seconds, then writes the number to the response. Great so far! No, try this: Go to Wait.aspx and while it's spinning, quickly tab over to NoWait.aspx and refresh. Even while Wait.aspx is sleeping, NoWait.aspx WILL provide a response. Great so far. You can continue to refresh NoWait.aspx while Wait.aspx is spinning, and the server happily sends a response each time. This is the behavior I expect.
Now is where it gets weird.
In a 3rd tab, in the same browser, visit SessionStart.aspx. Next, tab over to Wait.aspx and refresh. While it's spinning, tab over to NoWait.aspx and refresh. NoWait.aspx will NOT send a response until Wait.aspx is done running!
This proves that while a session is active, you can't make concurrent requests with the same user. Requests are all queued up and served synchronously. I do not expect or understand this behavior. I have tested this on Visual Studio 2008's built in web server, and also IIS 7 and IIS 7.5.
So I have a few questions:
1) Am I correct that there is indeed a limitation here, or is my test above invalid because I am doing something wrong?
2) Is there a way to work around this limitation? In my web app, certain things take a long time to execute, and I would like users to be able to do things in other tabs while they wait of a big request to complete. Can I somehow configure the session to allow "dirty reads"? This could prevent it from being locked during the request?
3) Why does this limitation exist? I would like to gain a good understanding of why this limitation is necessary. I think I'd be a better developer if I knew!
Here is a link talking about session state and locking. It does perform and exclusive lock.
The easiest way around this is to make the long running tasks asynchronous. You can make the long running tasks run on a separate thread, or use and asynchronous delegate and return a response to the browser immediately. The client side page can send requests to the server to check and see if it is done (through ajax most likely), and when the server tells the client it's finished, notify the user. That way although the server requests have to be handled one at a time by the server, it doesn't look like that to the user.
This does have it's own set of problems, and you'll have to make sure that account for the HTTP context closing as that will dispose certain functionality in the asp.net session. One example you'll probably have to account for is probably releasing a lock on the session, if that is actually occurring.
This isn't too surprising that this could be a limitation. Each browser would have it's own session, before the advent of ajax, post back requests were synchronous. Making the same session handle concurrent could get really ugly, and I can see how that wouldn't be a priority for the IIS and ASP.NET teams to add in.
For reasons Kevin described, users can't access two pages that might write to their session state at the same time - the framework itself can't exert fine-grained control over the locking of the session store, so it has to lock it for entire requests.
To work around this, pages that only read session data can declare that they do so. ASP.NET won't obtain a session state write lock for them:
// Or false if it doesn't need access to session state at all
EnableSessionState="ReadOnly"
Is there a way to do a Session.Abandon() when the user just kills the session by X'ing out with the little red X box in the upper right corner? I am seeing situations where after an "X out", the user comes back in and it seems like session variables are still present? I guess explorer keeps them intact? (I am unsure of this).
You can try doing an AJAX type callback in the OnUnload event - however, as someone else mentioned you'd have to be aware of other tabs being open (not easy), and it still won't guarantee you get that event.
There's a combination of things to do to get a similar type of effect.
Session Cookie should have a null/empty expiry time. This ensures the browser deletes the session from it's end after the browser is closed.
The ASP Session can be set with a short SessionState timeout value. This means if there's no client activity within that period, the Session will expire.
The side effect of this is that if a user is just looking at the site, and not performing activity (regardless of whether the browser is still open) - the session can expire.
This can be worked-around by having a Javascript timer periodically ping back to the server with an AJAX call or similar. The side effect of THIS is that it generates more site traffic.
The server is typically not aware of such events on the client. The only way the server can be notified about anything is if there is a request sent back to it. I guess you could create such a notification in a JavaScript, but then you should also keep in mind that the session in mind that the session is not per-page but (usually) per user, which means that you would need to keep track of how many tabs/windows the user has opened so that you don't kill the session when you should not.
Personally, I usually try to design the web apps so that they live well with the default handling of sessions, either providing a specific "Logout" command that will kill the session, or simply let it hit the timeout and die.