为什么失败的ManualResetEvent在使用Silverlight 4此同步调用工作? [英] Why does ManualResetEvent fail to work in this synchronous call using Silverlight 4?

查看:130
本文介绍了为什么失败的ManualResetEvent在使用Silverlight 4此同步调用工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们放下的那一刻,在<一个href=\"http://stackoverflow.com/questions/647481/how-i-can-implement-sync-calls-to-wcf-services-in-silverlight\">question是否同步般的操作应该甚至Silverlight应用程序的范围内尝试。如果我在下面code使用的ManualResetEvent为:

Let's put aside for the moment, the question 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:

    static string result;
    static AutoResetEvent are = new AutoResetEvent(false);
    static ManualResetEvent mre = new ManualResetEvent(false);
    public static string AsyncCall()
    {
        string url = "http://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();
    }

正如你从阅读 ManualResetEvent的MSDN上,当控制线程完成活动中,它调用设置方法,以表示该等待的线程才能继续。,当设置()被调用w_DownloadStringCompleted,控制返回到开始AsyncCall等待等待线程。这与.NET 4.0运行此时会发生什么。在AsyncCall线程被阻塞,直到下载完成设置被调用。

As you'd expect from reading ManualResetEvent on MSDN, "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.

如果我跑在同一块在Silverlight 4 code的,DownloadStringAsync将调用,但控制不会到达w_DownloadStringCompleted回调。一旦WaitOne的()被调用,在AsyncCall该线程只是挂在那里,并脱下来处理DownloadStringAsync线程永远不会到达回调。我见过一个线程在SL4到达下载回调的唯一方法是,如果从AsyncCall线程从AsyncCall返回。所以设置()不会被调用。

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.

为什么不ManualResetEvent的工作预期在Silverlight 4?为什么它从.NET 4有什么区别?
这也许是异步设计模式,微软的执行?
或者是有什么我失踪?

Why doesn't ManualResetEvent work as expected in Silverlight 4? 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?

感谢

推荐答案

在Silverlight中的网络回调总是会到来的在同一个线程的,因为他们的发源地。例如,如果你犯了一个WebClient.DownloadStringAsync在UI线程上(即,在一个按钮单击事件),那么回调的通话将被排队等待同一个UI线程上发表。但是,您的来电 mre.WaitOne()阻塞UI线程,因此回调是永远不会被调用,而 mre.Set()通话从未发生过。

The networking callbacks in Silverlight will always arrive in the same thread as they were originated. For example, if you make a WebClient.DownloadStringAsync on the UI thread (i.e., on a button click event), then the callback call will be queued to be delivered on the same UI thread. However, your call to mre.WaitOne() is blocking the UI thread, so the callback is never invoked, and the mre.Set() call never happens.

所以,是的,这是在网络调用​​的异步性的一种执法 - 你真正的不能的做到同步调用,即使他们似乎是同步的。

So yes, that's a kind of enforcement in the asynchronicity of networking calls - you really can't do synchronous calls, even if they seem to be synchronous.

这篇关于为什么失败的ManualResetEvent在使用Silverlight 4此同步调用工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆