0

I'm working with async-await methods and I can't get next functionality:

I have a sync wrapper method (MethodSync) that calls an async method (MethodA_Async) and then does some stuff (MethodB).

After that, it enters to a loop where for each item it does some stuff (MethodC) and then an async method (MethodE_Async) is run but waiting for MethodA_Async to finish (that is why I pass the task that MethodA_Async returns: to await for it to finish).

Finally, it performs other stuff (MethodD) and waits for all MethodE_Async to finish.

The problem is that it is not working and it enters to an infinite loop of waiting.

private async Task<bool> MethodA_Async()
{
   bool finish = false;
   await Task.Run(()=>
   {
       // Doing another things
       finish = true;
   });
   return finish;
}
private void MethodB() { }

private void MethodC() { }

private void MethodD() { }

private async Task MethodE_Async (Task<bool> methodATask, int item)
{
   await Task.Run(async() =>
   {
      // Waits for MethodA_Async to finish
      bool finish = await methodATask;

      // Other logic that works on item parameter.
      var result = 2*item;

   });
}

public bool MethodSync()
{
   // Starts MethodA_Async
   var taskMethodA = MethodA_Async();

   // Doing other things that do not depend upon MethodA_Async
   MethodB();

   // Iterations
   var tasksDependingOnMethodA = new List<Task>();
   var items = new List<int>{ 1, 2, 3 };
   foreach(var item in items)
   {
      // Doing other things that do not depend upon MethodA_Async
      MethodC();

      // MethodE_Async starts
      tasksDependingOnMethodA.Add(MethodE_Async(taskMethodA, item));
   }

   // Other method that does not depend upon MethodA_Async
   MethodD();

   // Wait for all MethodE_Async methods to finish
   Task.WhenAll(tasksDependingOnMethodA).GetAwaiter().GetResult();

   return true;
}

I'd appreciate any guidance you could give me.

Carlos
  • 786
  • 7
  • 13
  • Your code sample doesn't compile. Please provide a [mcve]. – mjwills Jun 16 '20 at 04:22
  • Why does `MethodE_Async` need `Task.Run`? – mjwills Jun 16 '20 at 04:24
  • I'm trying to start running async logic inside MethodE_Async. – Carlos Jun 16 '20 at 04:32
  • 1
    You already have the `await` there - I am pretty sure the async await state machine will already be being used? – mjwills Jun 16 '20 at 04:36
  • 2
    Your code may deadlock under certain conditions. It looks like you want to run async methods from a synchronous method. **A general solution does not exist**. The only correct way to invoke async methods is to have an entirely async-ready call-site (there are edge-cases, such as `async void` event-handlers, but that's only because there's special async-aware logic in WinForms and WPF contexts, no such features exist in library code). – Dai Jun 16 '20 at 04:40
  • 1
    @CarlosPozos It is already asynchronous without `Task.Run`. `Task.Run` starts work *in parallel* (on another thread), which is different than "asynchronous". It just gives you an asynchronous way to wait for that thread to complete. – Gabriel Luci Jun 17 '20 at 02:02
  • Many thanks you all. I've found the problem and it was because of the `Task.Run`. I was not understanding the concept. – Carlos Jun 17 '20 at 06:19

0 Answers0