When a method is Async? RRS feed

  • Question

  • In another post I was answered that it is a good idea to expose sync and async methods in my class library because it is good that is the client that uses the library decide what kind of method to use. Now my doubt is to know when a method is considerated async.

    For example i know that is an async method:

    private async Task<bool> miMethod01Async()

    //long time code

                return await Task<bool>.Run(() =>
                    for (Int64 i = 1; i < 100000000; i++)
                        //Do something

                    return true;
                }); }

    It is an async method because it use a task.

    However, if I am not wrong, one of the reasons to let the client to decide the kind of method to use it is because of the use of threads, for example in ASP.NET decide if to use threads or not.

    So if I use parallelism, that really it doesn't use a task, is it a async method too? For example this method.

    public List<int> orderList(List<int> paramList)
                return paramList.AsParallel().OrderBy(x => x).ToList();

    In this case, the method is not awaitable but it can use many threads, so it could be a problem for scalability reasons like in ASP.NET.

    So the question is, should I considerate both methods async or only the first one? Should I difference the second method in some way, for example indicate "parallel" to indicate that it could use many threads?


    Friday, October 20, 2017 3:16 PM


All replies

  • To me, it looks as though your orderList method is still going to return synchronously.

    Just because it uses the AsParallel method inside it (though I can't see if that's actually going to do anything useful in your particular example) doesn't mean that the entire method is async.

    For example, say you call a method - that method may do 10 different calculations in parallel but only returns once all 10 calculations have completed and it can compute a final answer that combines all 10 calculations.

    As far as the calling code is concerned that method is synchronous - it called it and it waited for the final answer. The calling code has no knowledge (and shouldn't have any knowledge) of how that calculation is performed. It doesn't matter what the method does internally - if it doesn't return until it has fully completed then it is synchronous.

    A method that indicates it is asynchronous returns a task because that tells the calling code that the method has not (or may not) have completed yet but is off doing something. Therefore, here is a task that will tell you when I'm done.

    Friday, October 20, 2017 3:39 PM
  • And parallel.foreach use many threads?

    I mean that I know ASP can have problems with methods that use many threads, it is the reason why it is not a good idea to wrap a sync method into a task, because it uses a new thread and ASP don't know that and have performace problems.

    So if I am using a parallel.foreach and use many threads, will I have the same problem that wraping a synch method into a task?


    Tuesday, October 31, 2017 10:39 AM
  • Hi ComptoAlvaro,

    >>And parallel.foreach use many threads?

    All public and protected members of Parallel are thread-safe and may be used concurrently from multiple threads.

    For the relationship between and parallel, you could refer to the post in StackOverFlow. It gives a good explanation.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, November 7, 2017 10:10 AM