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方法,然後等待直到所有人都完成了,或者我理解你的問題是錯的? –