How can I do an asynchronous Submit Changes when the window is being closed in Silverlight

eb8931
eb8931 used Ask the Experts™
on
Hi,

How can I ensure that all changes have been saved back to the web server (RIA) when the user exits from a silverlight application.  Calling SubmitChanges, which is asynchonous, from the App_Exit event handler doesn't work since (I assume) the application terminates before the server has time to process the changes.

I tried the following but the Lock on the main thread appears to lock SubmitChanges before it can submit to the server (the timer runs but keeps reporting that IsSubmitting is still true and nothing gets run on the server).

 
void App_Exit(object sender, EventArgs e) {
      if (MyRIAServices.HasChanges) {
        var SubmitChangesWorker = new BackgroundWorker();
        SubmitChangesWorker.DoWork += new DoWorkEventHandler(SubmitChangesWorker_DoWork);
        SubmitChangesWorker.RunWorkerAsync();
        Lock.WaitOne();
      }
    }

    void SubmitChangesWorker_DoWork(object sender, DoWorkEventArgs e) {
      MyRIAServices.SubmitChanges();
          
      var WaitForSubmit = new Timer(WaitForSubmit_Tick, null, new TimeSpan(0, 0, 0, 0, 100), new TimeSpan(0, 0, 0, 0, 100));
     
    }

    void WaitForSubmit_Tick(object State)
      if (!MyRIAServices.IsSubmitting)
        Lock.Set();

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. Software Engineer / Architect
Commented:
It seems that service calls are blocked from app.exit event by design.  

Aaron Edwards has a javascript interop workaround that does work (for me).  

Here is the link (his post is about half-way down the page):
http://forums.silverlight.net/forums/p/20279/69788.aspx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial