Hey gang,
I just published a new article showing how to implement a clean shutdown sequence in a Silverlight and WPF application. The sample app uses the MVVM Light Toolkit Messenger, but of course this could also be implemented with other kind of messengers.
I have been confronted to this kind of issues in production applications in the past, with a lot of components having to shut down in a coordinated manner, and I think that having a decoupled messenger makes that issue much, much less difficult to tackle (and much, much easier to extend when new components are added).
http://blog.galasoft.ch/archive/2009/10/18/clean-shutdown-in-silverlight-and-wpf-applications.aspx
Hope you like it,
Laurent
--
Laurent Bugnion [Microsoft MVP, MCP]
Blog: http://blog.galasoft.ch
Support children in Calcutta: http://www.calcutta-espoir.ch
|
as |
Laurent,
I've only barely skimmed the article just now, but one of the biggest questions I've seen for Silverlight shutdown is how to do pretty much anything if the user decides to close by hitting the *browser's* close button. I've not tried this myself, but people seem to find some difficulty doing things like web service calls, etc. before their application gets terminated. Does your solution address this? If so, I think it would be worth calling specific attention to that in light of the interest I've seen already.
Thanks!
-Corrado
On Mon Oct 19 02:38:56 CDT 2009, David Anson
<davi...@microsoft.com> wrote:
> Blog: http://blog.galasoft.ch<http://www.galasoft.ch/>
> Web: http://www.galasoft.ch<http://www.galasoft.ch/>
> Support children in Calcutta:
> http://www.calcutta-espoir.ch<http://www.calcutta-espoir.ch/>
>
> [cid:image0...@01C9C8AA.B722DA80]
>
> My<http://galasoft.ch/contact/mobile/>
> business<http://galasoft.ch/contact/mobile/>
> card<http://galasoft.ch/contact/mobile/>
> as
> Microsoft Tag<http://www.microsoft.com/tag>
>
>
>
>
Hi David,
No, the solution proposed here concentrates on taking actions when a user clicks on a button. For Silverlight to do something when the user closes the browser is an old problem (and it is not specific to Silverlight, we had this issue already in JavaScript back then). Unfortunately, even though Silverlight has a “Application.Exit” event, as far as I know there is no guarantee that operations executed there will be really performed, especially not asynchronous operations (but it will be called when the browser gets closed, so you can at least attempt to do something. Note that Application.Exit cannot be cancelled.
Also, in the contrary to WPF, there is no equivalent Window.Closing (that can be cancelled).
It’s a good input, I will update the article to mention this. Thanks!!
Laurent
Invoking JS when the browser is closing is very likely to fail. When I was
still very active in JavaScript (yes, this is one dark aspect of my past,
generally unknown ;) the specs of the browsers specified that nothing was
guaranteed to be executed if the browser was closing down, especially not
async operations such as web service calls.
If the intent is to log when a user is leaving a page or closing down, the
only viable way is to have a heartbeat ping the web server at regular
intervals, and log when the heart beat stops.
Cheers,
Laurent
-Corrado
-----Original Message-----
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
On Behalf Of Daniel Vaughan
Sent: martedì 20 ottobre 2009 00:13
To: WPF Disciples
Subject: [WPF Disciples] Re: Clean shutdown in WPF and SL using a Messenger
The conversation on this thread led me to do a little experimenting this
evening. So I thought I would share it with you guys. There are two close
interception points that I have examined. The first, and as Laurent has
said, unreliable interception point occurs after the Silverlight App.Exit is
raised; which I had a crack at using Ajax. And the more reliable
interception point uses the browsers onbeforeunload event to assign a string
to the event.returnValue, thereby giving time to dispatch a web service
call.
http://danielvaughan.orpius.com/post/Calling-Web-Services-from-Silverlight-a
fter-the-Browser-has-closed.aspx
Cheers,
Daniel
On Oct 19, 10:25 am, "Laurent Bugnion, GalaSoft" <laur...@galasoft.ch>
wrote:
> On Mon Oct 19 02:38:56 CDT 2009, David Anson <david...@microsoft.com>
> http://blog.galasoft.ch/archive/2009/10/18/clean-shutdown-in-silverli...
> -wpf-applications.aspx
>
> > Hope you like it,
> > Laurent
> > --
> > Laurent Bugnion [Microsoft MVP, MCP]
> >Blog:http://blog.galasoft.ch<http://www.galasoft.ch/>
> > Web:http://www.galasoft.ch<http://www.galasoft.ch/>
> > Support children in Calcutta:
> >http://www.calcutta-espoir.ch<http://www.calcutta-espoir.ch/>
>
> > [cid:image001....@01C9C8AA.B722DA80]
I didn't try, but i think it will be exactly the same. One of the difficulties of this exercise is that it is impossible to distinguish between the page being navigated away from, and the browser being shut down, because the events involved are exactly the same.
The article is great, and it would be very interesting to test on multiple browsers to see how they react. One caveat of using onbeforeunload in JavaScript is that IIRC it is not a standard event (or at least it was not when i used to write JavaScript code) so it might fail in some browsers (ah the joy of cross browser coding, i miss that... NOT)
Cheers,
Laurent
--
Sent from mobile