Clean shutdown in WPF and SL using a Messenger

125 visualizações
Pular para a primeira mensagem não lida

Laurent Bugnion, GalaSoft

não lida,
18 de out. de 2009, 15:56:5318/10/2009
para wpf-di...@googlegroups.com

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

Web: http://www.galasoft.ch

Support children in Calcutta: http://www.calcutta-espoir.ch

 

cid:image001.png@01C9C8AA.B722DA80

My

business

card

as

Microsoft Tag

 

 

image001.png

David Anson

não lida,
19 de out. de 2009, 03:38:5619/10/2009
para wpf-di...@googlegroups.com

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 Cavalli

não lida,
19 de out. de 2009, 03:52:3519/10/2009
para wpf-di...@googlegroups.com
AFAIK the only way to handle browser close or navigation to
another site is to use IApplicationService (Silverlight3)
unfortunately calls to webservices don't work.
Solution we've considered (but not tried) is to use JS/JQuery to
invoke it.

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

Laurent Bugnion, GalaSoft

não lida,
19 de out. de 2009, 03:53:4219/10/2009
para wpf-di...@googlegroups.com

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

image001.png

Laurent Bugnion, GalaSoft

não lida,
19 de out. de 2009, 04:25:0719/10/2009
para wpf-di...@googlegroups.com
Hey Corrado,

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

Daniel Vaughan

não lida,
19 de out. de 2009, 18:13:0919/10/2009
para WPF Disciples
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-after-the-Browser-has-closed.aspx

Cheers,
Daniel


On Oct 19, 10:25 am, "Laurent Bugnion, GalaSoft" <laur...@galasoft.ch>
wrote:
> Hey Corrado,
>
> 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
>
> -----Original Message-----
> From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
>
> On Behalf Of Corrado Cavalli
> Sent: Monday, October 19, 2009 9:53 AM
> To: wpf-di...@googlegroups.com
> Subject: [WPF Disciples] Re: Clean shutdown in WPF and SL using a Messenger
>
> AFAIK the only way to handle browser close or navigation to
> another site is to use IApplicationService (Silverlight3)
> unfortunately calls to webservices don't work.
> Solution we've considered (but not tried) is to use JS/JQuery to
> invoke it.
>
> -Corrado
>
> On Mon Oct 19 02:38:56 CDT 2009, David Anson
> 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]

Jeremiah Morrill

não lida,
19 de out. de 2009, 18:21:2819/10/2009
para wpf-di...@googlegroups.com
Great article Daniel!  +5, insightful

Daniel Vaughan

não lida,
19 de out. de 2009, 18:30:2219/10/2009
para WPF Disciples
Thanks Jer!

On Oct 20, 12:21 am, Jeremiah Morrill <jeremiah.morr...@gmail.com>
wrote:
> Great article Daniel!  +5, insightful
>
> On Mon, Oct 19, 2009 at 3:13 PM, Daniel Vaughan <dbvaug...@gmail.com> wrote:
>
> > 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-Silver...

David Anson

não lida,
19 de out. de 2009, 19:07:5219/10/2009
para wpf-di...@googlegroups.com
I've put this up on Twitter for whatever it's worth:
http://twitter.com/DavidAns/status/5003267725

Daniel Vaughan

não lida,
19 de out. de 2009, 19:18:4419/10/2009
para WPF Disciples
Thanks David.
It will be good to see if anyone has an alternative approach.

Cheers,
Daniel

On Oct 20, 1:07 am, David Anson <david...@microsoft.com> wrote:
> I've put this up on Twitter for whatever it's worth:http://twitter.com/DavidAns/status/5003267725
>
> -----Original Message-----
> From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Daniel Vaughan
> Sent: Monday, October 19, 2009 3:13 PM
> 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-Silver...

Corrado Cavalli

não lida,
20 de out. de 2009, 00:31:4820/10/2009
para wpf-di...@googlegroups.com
Superb article Daniel!
Does that work even when user navigates away from the application?
(wondering about 2nd approach...)

-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]

Laurent Bugnion

não lida,
20 de out. de 2009, 01:36:4620/10/2009
para wpf-di...@googlegroups.com
Disclaimer: i didn't write serious JavaScript code for quite some time so things might not be as i remember them anymore ;)

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

Daniel Vaughan

não lida,
20 de out. de 2009, 02:47:3320/10/2009
para WPF Disciples
Thank you kindly Corrado.

I've tested it with IE8 and FF3.5.3, and the second method worked
locally when the user navigates away using a bookmark, url in
navigation bar, and a hyperlink on the page.

Cheers,
Daniel

On Oct 20, 6:31 am, "Corrado Cavalli" <corradocava...@gmail.com>
wrote:
> Superb article Daniel!
> Does that work even when user navigates away from the application?
> (wondering about 2nd approach...)
>
> -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-Silver...

Daniel Vaughan

não lida,
20 de out. de 2009, 02:50:4720/10/2009
para WPF Disciples
Thanks Laurent.
For sure cross browser support needs evaluating further.
> http://danielvaughan.orpius.com/post/Calling-Web-Services-from-Silver...
Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem