What is difference between Async and Task

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
see this code example

public Task<List<string>> GetCitiesAsync()
{
GetCities.GlobalWeather list = new GetCities.GlobalWeather();
string Data = list.GetCitiesByCountry("India");
XmlDocument xmlDoc = newXmlDocument();
xmlDoc.LoadXml(Data);
XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("//Table");
List<string> Listcity = new List<string>();

foreach (XmlNode node in nodes)
{
Listcity.Add(node["City"].InnerText);
}
return Task.FromResult(Listcity);
}

public async Task<ActionResult> GetCitiesByCountryAsync() // Asynchronous Action Method
{
List<string> listofcities = await GetCitiesAsync();
return View(listofcities);
}



1) first function return Async and second one return Async Task<ActionResult>

so i like to understand the difference between Async and Task. i know Task class spawn new thread but Async does not.

2) see first example code which has no async keyword but when it is calling then await is used. when function is not async then how could i use await to call that function ?

please discuss elaborately to understand difference between Async and Task.

3) how to read return string value when function type is async and return string?

var task = _ModelManager.GetModelData(selectDate);
await Task.WhenAll(task);
string value= task.Result;

i am calling my function this way await Task.WhenAll(task); then i need to read return value by task.Result; ?

can i read return value without task.Result; ?

please share knowledge. thanks

Continue reading...
 
Back
Top