Where is asynchronous and waiting for the end? confusion

I have a program that has no purpose, but to help me understand how async works and wait. This is a console application that parses XML and expects the first name to be returned, either last name or first name. Here is the code:

static void Main(string[] args)
{
     Task<string> name = GetFirstParsedName();
     name.Wait();
     if (name.IsCompleted)
     {
         Console.WriteLine(name.Result);
     }

     Console.ReadLine();
 }

static async Task<string> GetFirstParsedName()
{
  string xmlSnippet = @"<person>
<FirstName>Chamir</FirstName>
<Surname>Bodasing</Surname>
<Gender>Male</Gender>
<Nationality>South African</Nationality></person>";

  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.LoadXml(xmlSnippet);

  XmlParser xmlParser = new XmlParser();
  Task<string> t_GetFirstName = xmlParser.GetFirstName(xmlDoc);
  Task<string> t_GetSurname = xmlParser.GetSurname(xmlDoc);  

  Task<string> t_firstReturnedName = await Task.WhenAny(new Task<string>[] { t_GetFirstName, t_GetSurname });

  string firstReturnedName = await t_firstReturnedName;
  return firstReturnedName;    
}

static async Task<string> GetFirstName(XmlDocument personXml)
{
  string firstName = personXml.SelectSingleNode("//FirstName").InnerText;
  await Task.Delay(5000);
  return firstName;
}

static async Task<string> GetSurname(XmlDocument personXml)
{
  string surname = personXml.SelectSingleNode("//Surname").InnerText;
  await Task.Delay(1);
  return surname;
}

It seems like it makes sense to use the async method when you don't need to return the value to the main method. If this does not mean setting a property of the global class that can be accessed. If not, to wait for a method, all methods must be asynchronous, which in turn means that the return type must be Task<T>. It seems like this never ends unless I explicitly have to write the following code (as in the main method):

Task<string> name = GetFirstParsedName();
name.Wait();
if (name.IsCompleted)
{
    Console.WriteLine(name.Result);
 }

, ? result , , .

+4
3

, async, .

? async, . , .

, , , , , "". , .

. Async plage, . ( Main ). async, . , , WebAPI, . , , .

, ? result , , .

Result, , Main async ( ASP.NET CoreCLR ). ASP.NET, await .

+5

, async, . , .

async Task<T>, . , (.. /); , (.. ).

, , , , , "". , .

async "async all the way". , . , " " async void ( ) async Task<T> ( ASP.NET). , Wait()/Result Main.

, async - . , async , , - ...

+6

, async let compiler , , , , , main ( UI).

async-wait, :

, "" WinForm, .

:

    private Task SomeUIOperation()
    {
        // ui operation
        return Task.Run(() =>
        {
            this.Invoke(new Action(() => this.BackColor = Color.Aquamarine));
            Thread.Sleep(10000);
            this.Invoke(new Action(() => this.BackColor = Color.Gray));
        });
    }

    private async void button1_Click(object sender, EventArgs e)
    {
        await SomeUIOperation();

        // some other stuff
    }

async-await, 10 .

, async-wait, , , - , , .

And the console application is not the best type of project for testing and training Async-Await

+1
source

Source: https://habr.com/ru/post/1611187/


All Articles