locked
Writing a custom async task RRS feed

  • Question

  • User1352447851 posted

    I want to get a list of Test asynchronously, however I am stuck on writing the class. How do I create my own asynchronous method?

    In my page model:

    public IList<Test> Test { get; }

     public async Task OnGetAsync()

            {

                CustomClass customclass = new CustomClass();

                Test = await customclass.getDetails();   

            }

    In my custom class:

    public Task<IList<Test>> getDetails()

            {

                List<Test> tests = new List<Test>();

                Search search = new Search();

                List<string> items = search.getItems();

                foreach (var item in items)

                {

                  Test test = new Test();

                  test.details = item.detail;

                  tests.Add(test);

               }

    return tests; //wrong return type - needs to be a task

    Tuesday, February 19, 2019 2:12 PM

Answers

  • User-893317190 posted

    Hi Shadow_Kittencorn,

    Asynchronous in server side means you don't need to wait the task to finish and you could run other code while the task is running.

    According to your description,your client needs a list to populate a dropdownlist.

    Since the List<Test> is your final target, no matter you use task or not , you still need to wait the List<Test> to return,which has no difference from write a non-async method.

    When to use async? Here is a small sample.If you have two task , one retrieves data from database , say it is taskForDataBase, another  task does some calculations, say it is calculationTask.

    Assume taskForDataBase costs 10s , calculationTask costs 10s.

    When you run taskForDataBase  asynchronously,  you could do calculationTask  at the same time. Then the total costs is still about 10s.

    If you run taskForDataBase   synchronously, you should wait for the taskForDataBase to run calculationTask , which finally will costs 20s.

    In this scene, you could make taskForDataBase  asynchronous.

    If all you want to get is the result of  getDetails,, you still  need to wait for the task to finish.

    PatriceSc has mentioned ajax.

    So, if your getDetails really cost a lot of time, you could show loading message and at the same time use ajax to retrieve data from database.

    If it doesn't cost a lot of time, it is not necessary to use async method.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2019 3:21 AM

All replies

  • User753101303 posted

    Hi,

    The primary use case for async/await should be to call existing input/output async APIs. See for example http://www.entityframeworktutorial.net/EntityFramework6/async-query-and-save.aspx (edit: though I don't like the last code sample).

    You'll need to use:

    public async Task<IList<Test>> getDetailsAsync()

    The async keyword is needed so that you can use await and have this method return Task<T> when you return a T typed value (BTW note that your return statement is currently inside the loop). The Async suffix is just a convention to clarify that this is an Async method.

    I would suggest to NOT try to turn your code to an async method if it is not truly async (ie using Task.Run for I/O bound code). search.getItem() is reading from a database ?

    Tuesday, February 19, 2019 2:38 PM
  • User1352447851 posted

    search.getItem()

    Is indeed reading data from a database.

    What I am trying to do is pull values from an external source and use them to populate a form dropdown box.

    As the page needs to 'wait' before displaying the form, I assumed it would need to be async?

    Sorry, I cut of some of the unnecessary code and so it should not have been in a loop. I will change.

    Tuesday, February 19, 2019 4:40 PM
  • User753101303 posted

    Not 100% sure to see what you mean by "needs to wait". I would do this the other way round ie if you see an existing XXXAsync API you could call then you are creating an async method so that you can call await XXXAsync from your code.

    It shouldn't be quicker but should help with scalability. Rather than having the CPU to wait for I/O completion, it waits using a specialized mechanism making the CPU free to process other CPU bound works...

    A common confusion is that Ajax requests are async by default (but client side and server side async is basically unrelated).

    Tuesday, February 19, 2019 4:54 PM
  • User-893317190 posted

    Hi Shadow_Kittencorn,

    Asynchronous in server side means you don't need to wait the task to finish and you could run other code while the task is running.

    According to your description,your client needs a list to populate a dropdownlist.

    Since the List<Test> is your final target, no matter you use task or not , you still need to wait the List<Test> to return,which has no difference from write a non-async method.

    When to use async? Here is a small sample.If you have two task , one retrieves data from database , say it is taskForDataBase, another  task does some calculations, say it is calculationTask.

    Assume taskForDataBase costs 10s , calculationTask costs 10s.

    When you run taskForDataBase  asynchronously,  you could do calculationTask  at the same time. Then the total costs is still about 10s.

    If you run taskForDataBase   synchronously, you should wait for the taskForDataBase to run calculationTask , which finally will costs 20s.

    In this scene, you could make taskForDataBase  asynchronous.

    If all you want to get is the result of  getDetails,, you still  need to wait for the task to finish.

    PatriceSc has mentioned ajax.

    So, if your getDetails really cost a lot of time, you could show loading message and at the same time use ajax to retrieve data from database.

    If it doesn't cost a lot of time, it is not necessary to use async method.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2019 3:21 AM