0

将服务引用添加到我的电话应用程序(例如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???
    }
}
4

1 回答 1

0

在 WP7 中,HTTP 响应在 UI 线程上处理。阻塞 UI 线程会阻止响应被处理。

于 2011-09-30T13:27:06.183 回答