Updating a table when browser closes?


  •  In the app.xaml.cs file I have this code:
            private void Application_Exit(object sender, EventArgs e)
                Service1Client webService = new Service1Client();
                if (WebSessions.Session.ContainsKey("uid"))
                    webService.UpdateUserLoginStatusCompleted += new EventHandler(webService_UpdateUserLoginStatusCompleted);
                    webService.UpdateUserLoginStatusAsync(Convert.ToInt32(WebSessions.Session["uid"]), 0);


     I ran the through some debugging and it gets to the

     webService.UpdateUserLoginStatusAsync(Convert.ToInt32(WebSessions.Session["uid"]), 0);
    but it will never actually update. am I going about this the wrong way? And if it's a per browser basis how do I handle it with IE and Firefox? 
    Wednesday, February 04, 2009 1:02 PM


All replies


    longstanding feature request:

    there's a workaround here:

    you shouldn't heavily rely on this logic.  if the browser is end-task'd, crashes, etc, or the user's pc crashes or loses power, the event will not fire.

    Wednesday, February 04, 2009 1:49 PM
  • I believe that as soon as that method ends the application will close. So if you will never hit the completed event and its possible it closes the connection right away so the web service call is killed before it is processed. You would need to block on the thread until the web service call comes back. Some examples of how to do this here.

    Now your users are probably not going to want to wait for your table to get updated to navigate away from the page or close their browser. So make sure your wcf call is defined as one way and that you don't make the user wait for the method to complete.

    Wednesday, February 04, 2009 1:49 PM
  •  I've tried the WaitOne approach I wasn't able to get it working. My browser would lock up and I would have to CTRL+ALT+DEL end task on firefox. I didn't test it in IE so i'm not sure if its just Firefox doing it.

    I'm just thinking off the top of my head here, but if you used Java to catch the close browser event would you still be able to call a webService?

    Wednesday, February 04, 2009 2:43 PM
  • I think it is a timing issue and Java probably will only make it worse. I believe your best bet would be to use the method explained in the post suggested by ccombs above.

    Wednesday, February 04, 2009 4:23 PM