Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does ManualResetEvent fail to work in this synchronous call using Silverlight 4?
    primarykey
    data
    text
    <p>Let's put aside for the moment, the <a href="https://stackoverflow.com/questions/647481/how-i-can-implement-sync-calls-to-wcf-services-in-silverlight">question</a> of whether synchronous-like operations should even be attempted within the context of a Silverlight app. If I use ManualResetEvent as in the following code:</p> <pre><code> static string result; static AutoResetEvent are = new AutoResetEvent(false); static ManualResetEvent mre = new ManualResetEvent(false); public static string AsyncCall() { string url = "https://stackoverflow.com/feeds/tag/silverlight"; WebClient w = new WebClient(); w.DownloadStringCompleted += new DownloadStringCompletedEventHandler(w_DownloadStringCompleted); w.DownloadStringAsync(new Uri(url), url); mre.WaitOne(); return result; } static void w_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { result = e.Result; mre.Set(); } </code></pre> <p>As you'd expect from reading <a href="http://msdn.microsoft.com/en-us/library/2ssskfws%28v=VS.95%29.aspx" rel="nofollow noreferrer">ManualResetEvent on MSDN</a>, "When the controlling thread completes the activity, it calls the Set method to signal that the waiting threads can proceed.", when Set() is called in w_DownloadStringCompleted, control returns to the waiting thread that started waiting in AsyncCall. This is what happens when running this with .NET 4.0. The thread in AsyncCall gets blocked until the download completes and Set is called.</p> <p>If I run the same piece of code in Silverlight 4, DownloadStringAsync will get called, yet control will never reach the w_DownloadStringCompleted callback. Once WaitOne() is called, that thread in AsyncCall just hangs there, and the thread which took off to process DownloadStringAsync never reaches the callback. The only way I've seen a thread reach the download callback in SL4 is if the thread from AsyncCall returns from AsyncCall. So Set() never gets called.</p> <p><strong>Why doesn't ManualResetEvent work as expected in Silverlight 4?</strong> Why does it differ from .NET 4? Is this maybe Microsoft's enforcement of the asynchronous design pattern? Or is there something I'm missing?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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