0

I have a questions where If I run the Main method, with (GetRankedEditorsPickList) call inside it, it runs asynchronously but with (GetRankedEditorsPickList1) it runs sequentially. You can see the managed threadId and results.

Can someone give me an idea why?. This is linqpad code.

async Task Main()
    {
        List<string> y = new List<string>() { "a","b","c" };
                    var rankedFeedBatchTasks = y.Select(async (batch, index) =>
                    {
                        return new
                        {
                            Index = index,
                            RankedList = await GetRankedEditorsPickList1(batch).ConfigureAwait(false)
                        };
                    }).ToList();
         var rankedFeedBatches = await Task.WhenAll(rankedFeedBatchTasks);
         "Completed".Dump();
    }

    async Task<string> GetRankedEditorsPickList(string batch)
    {   
         $"Started + {Environment.CurrentManagedThreadId}".Dump();
         await Task.Delay(TimeSpan.FromSeconds(5));
         batch = batch + "1";   
         $"Ended + {Environment.CurrentManagedThreadId}".Dump();
        return batch;
    }

    // Define other methods and classes here
    Task<string> GetRankedEditorsPickList1(string batch)
    {
        $"Started + {Environment.CurrentManagedThreadId}".Dump();
        Thread.Sleep(TimeSpan.FromSeconds(5));
        batch = batch + "1";
        $"Ended + {Environment.CurrentManagedThreadId}".Dump();
        return Task.FromResult(batch);
    }

First method calls Results ::

Started + 11
Started + 11
Started + 11
Ended + 10
Ended + 13
Ended + 14
Completed

Second Method calls Results ::

Started + 11
Ended + 11
Started + 11
Ended + 11
Started + 11
Ended + 11

Completed

loneshark99
  • 706
  • 5
  • 16
  • `async` does not mean "concurrent" or "parallel": https://stackoverflow.com/questions/4844637/what-is-the-difference-between-concurrency-parallelism-and-asynchronous-methods – Dai Oct 12 '18 at 02:00
  • @Dai why first one is running in parallel because if I see the result, it starts all the 3 method calls, where as the lower one just starts 1 and then waits. – loneshark99 Oct 12 '18 at 02:03
  • you lost me at "if I replace the first function" - try and explain more clearly – jazb Oct 12 '18 at 02:11
  • useful: https://stackoverflow.com/questions/4844637/what-is-the-difference-between-concurrency-parallelism-and-asynchronous-methods?noredirect=1&lq=1 – jazb Oct 12 '18 at 02:16

3 Answers3

2

GetRankedEditorsPickList is an asynchronous function whereas GetRankedEditorsPickList1 is not. That's why the latter runs sequentially.

More specifically:

  • unlike Task.Delay, Thread.Sleep is not asynchronous.
  • Task.FromResult is also not asynchronous. It just creates an already completed task from a result that has already been computed. Computed, in this case, synchronously. In other words, it gives your method a signature as if it is an asynchronous function when in fact it isn't.
pere57
  • 652
  • 9
  • 18
1

Recommend you to read "Concurrency in C# Cookbook", by Stephen Cleary , In this book is given explanation and the way of using Task.FromResult

Problem You need to implement a synchronous method with an asynchronous signature. This situation can arise if you are inheriting from an asynchronous interface or base class but wish to implement it synchronously. This technique is particularly useful when unit testing asynchronous code, when you need a simple stub or mock for an asynchronous interface.

Solution You can use Task.FromResult to create and return a new Task that is already completed with the specified value

Task.FromResult(batch) return a completed task , that is why GetRankedEditorsPickList1 is executed synchronously

Z.R.T.
  • 1,543
  • 12
  • 15
0

async does not mean parallel. async is used to over come sequencial execution (i.e. if you want to execute some task not in sequencial way then you can use async)

  • Not quite. It’s entirely possible an **async** method can be executed concurrently by two different tasks thus parallel –  Oct 12 '18 at 02:27