void main() {
OpenWeatherApiClient.getCurrentLocationWeatherAsync(55.513434, -37.53434, (x) => {
Console.WriteLine(x.coord.lat);
Console.ReadLine();
});
}
Console.WriteLine("this should happen before");
/// I want to hold the the function here till the call back returns and executes the above logic
/// the asynchronous function
public static void getCurrentLocationWeatherAsync(double latitude, double longitude, Action<WeatherData> callback) {
//weather from one station
string weatherSearch = "weather?lat={0}&lon={1}";
var url = string.Concat(baseUrl, weatherSearch);
//Customize the URL according to the geo location
url = string.Format(url, latitude, longitude);
//Syncronous consumption
var asynClient = new WebClient();
//add Appid for verification
asynClient.Headers.Add(APPIDName, APPID);
asynClient.OpenReadCompleted += (o, a) => {
if (callback != null) {
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(WeatherData));
callback(ser.ReadObject(a.Result) as WeatherData);
}
};
asynClient.OpenReadAsync(new Uri(url));
}
我有女仆这个异步get方法,将请求解析成数据合同后。虽然我想持有主方法,直到委托中的getcall和相关代码完全执行。 未来我想要的是同时调用多个get方法,并保持它们直到所有方法都执行。 这之后,我将在主进行,因为每个GET调用数据取决于在主代码中等待,异步调用get方法执行
,你为什么要跑异步代码,当你真的想要执行线程? – poke
对不起,我没有明白。但我正在使用异步服务调用,因为我必须调用多个服务方法,并且不想等待第一个调用的响应,然后再继续执行第二个调用 – Ahsan
您想要多次调用getCurrentLocationWeatherAsync方法,然后等待直到所有人都完成了,或者我理解你的问题是错的? –