Windows Phone 7.1: AutoResetEvent不能与Service Methods一起工作
本文关键字:Service Methods 一起 工作 不能 AutoResetEvent Phone Windows | 更新日期: 2023-09-27 18:10:03
将Service Reference添加到我的手机应用程序(例如http://www.deeptraining.com/webservices/weather.asmx?op=GetWeather)后,我尝试使用AutoResetEvent进行仿真同步方法调用。但是在调用WaitOne之后,方法Set永远不会被调用。为什么?是bug吗?
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???
}
}
在WP7中,HTTP响应在UI线程上处理。阻塞UI线程会阻止响应被处理。