Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing LocalMessageSender synchronously
    text
    copied!<p>We have an application that has a primary window, it can launch multiple other windows, in new browsers. We are using a silverlight application as a coordinating server in the primary window to close all windows that are part of the app, regardless of the way they are opened (we can't guarantee it was via window.open so don't always have a handle to the window in javascript).</p> <p>On log out, we want to signal all the other windows to perform an auto-save, if necessary, then close down.</p> <p>So all windows have a silverlight app, they coordinate using localmessagesenders. However, these are asynchronous:</p> <pre><code>private void ProcessAutosave() { foreach (string s in _windows) { SendMessage(s, "notify-logout"); } // code here quoted later... } // sendasynch doesn't send until the method terminates, so have to do it in it's own function. private void SendMessage(string to, string message) { var lms = new LocalMessageSender(to); lms.SendCompleted += new EventHandler&lt;SendCompletedEventArgs&gt;(SenderSendCompleted); lms.SendAsync(message); } </code></pre> <p>Since the ProcessAutosave is called from a javascript onunload event which can't be cancelled, we need this to be synchronous and not complete before we have a response processed from each sub-window so the session state will still be valid etc.</p> <p>In the SenderSendCompleted we remove items from _windows when they have said they're done.</p> <p>So I added a loop on the end:</p> <pre><code>while(_windows.Count &gt; 0) { Thread.Sleep(1) } </code></pre> <p>However, that never terminates, unless I put an iteration counter on it.</p> <p>Am I the victim of a compiler optimisation meaning the changes in SenderSendCompleted do not affect that while loop, or, have I fundamentally misunderstood something? Or missed something obvious that's staring me in the face?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload