Windows Phone 7.1:AutoResetEvent不能与服务方法一起使用?

时间:2011-09-30 11:54:21

标签: c# windows-phone-7.1 autoresetevent

将Service Reference添加到我的Phone Application(例如http://www.deeptraining.com/webservices/weather.asmx?op=GetWeather)后,我尝试使用AutoResetEvent进行仿真同步方法调用。但在调用WaitOne之后,永远不会调用方法Set。为什么?这是一个错误吗?

public partial class MainPage : PhoneApplicationPage
{
    private readonly AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
    private string _result;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var weatherSoapClient = new WeatherSoapClient();

        weatherSoapClient.GetWeatherCompleted += weatherSoapClient_GetWeatherCompleted;
        weatherSoapClient.GetWeatherAsync("Pekin");

        _autoResetEvent.WaitOne(); // Program stop hire

        textBlock1.Text = _result;
    }

    void weatherSoapClient_GetWeatherCompleted(object sender, GetWeatherCompletedEventArgs e)
    {
        _result = e.Result;
        _autoResetEvent.Set(); // Never invoke! Why???
    }
}

1 个答案:

答案 0 :(得分:0)

在WP7中,HTTP响应在UI线程上处理。 Bocking UI线程可以防止响应被处理。