none
Using _ = with an Await/Async pattern RRS feed

  • Question

  • Okay  So I am pretty sure now that Await/Async works like this.  At the risk of confusing again with some pseudo code.

             Function one()    --Thread 1

             {

                    await Function Two   -- which will run in thread 2

             }

             async Task Function Two  -- Thread 2

             {

                   LongRunningFunction();   This is running on thread two

                   await Function Three              --Thread 3 runs Function Three

                   LongRunningFunction();   This is runs on thread two unless the await has the .ConfigAwait(false) in which case 

                                                         it will run on any available thread.

              }

              async Task Function 3 { await Task.Delay(5000); }

    I know that these come from the pool and the threads will be any free one.  Just keeping it simple ;-)

    So far so good.  Now to the question.

    In the function above if I used the syntax

                   _ = Function Three              

    instead of await Function Three.  Where does the code run.  If my understanding is right it runs on thread 2 however as Function 3 calls Task.Delay using an Await that task will run on another thread and Thread 2 would move on to the next LongRunningFunction as soon as the _ = Function Three call is made.  In this case it would make the Task.Delay pointless as nothing would wait for it.

    Am I now understanding this correctly?

    Saturday, July 27, 2019 1:45 PM

Answers

  • Yes, if I understand correctly, it will run on thread two.

    The difference between:

    a) var result = await RunMethod3()

    ...and...

    b) var result = RunMethod3()

    ...is that (b) returns the Task, not its result.

    for (b) you'd have so say:

    b) var result = RunMethod3().Result;

    ..or...

    b) RunMethod3().Wait();

    .. if it has no return.

    Testing with code-play is of course the quickest way to resolve such questions.

    Hope that helps/confirms?

    All the best,
    Pete


    #PEJL
    Got any nice code? If you invest time in coding an elegant, novel or impressive answer on MSDN forums, why not copy it over to TechNet Wiki, for future generations to benefit from! You'll never get archived again, and you could win weekly awards!

    Have you got what it takes o become this month's TechNet Technical Guru? Join a long list of well known community big hitters, show your knowledge and prowess in your favoured technologies!

    • Marked as answer by Tre4 Saturday, July 27, 2019 3:35 PM
    Saturday, July 27, 2019 2:02 PM
    Moderator

All replies

  • Yes, if I understand correctly, it will run on thread two.

    The difference between:

    a) var result = await RunMethod3()

    ...and...

    b) var result = RunMethod3()

    ...is that (b) returns the Task, not its result.

    for (b) you'd have so say:

    b) var result = RunMethod3().Result;

    ..or...

    b) RunMethod3().Wait();

    .. if it has no return.

    Testing with code-play is of course the quickest way to resolve such questions.

    Hope that helps/confirms?

    All the best,
    Pete


    #PEJL
    Got any nice code? If you invest time in coding an elegant, novel or impressive answer on MSDN forums, why not copy it over to TechNet Wiki, for future generations to benefit from! You'll never get archived again, and you could win weekly awards!

    Have you got what it takes o become this month's TechNet Technical Guru? Join a long list of well known community big hitters, show your knowledge and prowess in your favoured technologies!

    • Marked as answer by Tre4 Saturday, July 27, 2019 3:35 PM
    Saturday, July 27, 2019 2:02 PM
    Moderator
  • Here is an example that might help, load data from Entity Framework via an asynchronous call (.ToListAsync()) in a class which is called by a form in the Shown event.

    public async Task<List<CustomerEntity>> AllCustomersAsync(NorthWindAzureContext context)  
    { 
        _context = context; 
     
        var customerData  = await ( 
            from customer in context.Customers 
            join contactType in context.ContactTypes on customer.ContactTypeIdentifier 
                equals contactType.ContactTypeIdentifier 
            join contact in context.Contacts on customer.ContactId equals contact.ContactId 
            select new CustomerEntity 
            { 
                CustomerIdentifier = customer.CustomerIdentifier, 
                CompanyName = customer.CompanyName, 
                ContactIdentifier = customer.ContactId, 
                FirstName = contact.FirstName, 
                LastName = contact.LastName, 
                ContactTypeIdentifier = contactType.ContactTypeIdentifier, 
                ContactTitle = contactType.ContactTitle, 
                Address = customer.Address, 
                City = customer.City, 
                PostalCode = customer.PostalCode, 
                CountryIdentifier = customer.CountryIdentifier, 
                CountyName = customer.Country.Name 
            }).ToListAsync(); 
     
        return customerData; 
     
    }

    Data is read from the above in the Form Shown event.

    Shown event

    private async void Form1_Shown(object sender, EventArgs e)

    Reading the data in form shown.

    var result = await _operations.AllCustomersAsync(context) 
        .ConfigureAwait(false); 
     
    _customersView =  
        new BindingListView<CustomerEntity>(result);
    See the following post on ConfigureAwait in regards to preventing deadlocks. And Aync/Await best practices for asynchronous programming.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, July 27, 2019 2:32 PM
    Moderator
  • Thanks  Pete.  Sounds useful.  I've not heard of code-play, been just working it out myself with the internet.
    Saturday, July 27, 2019 3:36 PM
  • A much more thorough answer from Karen as always! 

    I ask you to mark Karen as joint answerer please Tre4.

    Good luck on your journey! It gets easier very quickly, once you grasp the thorny basics ;)

    All the best,
    Pete

    PS, sorry to drop random terms in there. Code-play to me just means knocking up a console app and trying it out with some throw-away code. Code for fun! <3


    #PEJL
    Got any nice code? If you invest time in coding an elegant, novel or impressive answer on MSDN forums, why not copy it over to TechNet Wiki, for future generations to benefit from! You'll never get archived again, and you could win weekly awards!

    Have you got what it takes o become this month's TechNet Technical Guru? Join a long list of well known community big hitters, show your knowledge and prowess in your favoured technologies!


    Saturday, July 27, 2019 4:02 PM
    Moderator