Transforming synchronous into asynchronous method

Asked

Viewed 710 times

3

I have the following method:

public string MyMethod1(string myParam1) {
    // Implementação
    return myReturnValue;
}

I needed to create an asynchronous method that did the same thing, to process several items from a list at the same time, so I created a second method as follows:

public async Task<string> MyMethod1Async(string myParam1) {
    return MyMethod1(myParam1);
}

Visual Studio complained that I’m not using the operator await in an asynchronous method. But basically I’m repurposing the method that already existed and creating an asynchronous alternative that I call as follows:

var myResult = Task.WhenAll(myStringList.Select(myStringItem => MyMethod1Async(myStringItem)).Result;

I used a string for the parameter only as a simplified example.

The synchronous method should continue to exist in the same way.


1. This way the objects in the actual list will be being processed to the same time?

2. Is there any more appropriate way to implement a method asynchronous in this context?

  • Have you looked at Task.FromResult<> will already solve your immediate problem, I do not know if you have something else, but, I believe that would solve

  • I didn’t know no @Virgilionovic , but I’ll take a look here right now. Thank you so much!

1 answer

2

  1. Yes, it is possible to have a synchronous method in an asynchronous process. What will happen? Several Task will be created and each task will run a synchronous process.

OBS: Sometimes there is no performance gain in such an environment, the important thing in an async process is to isolate it from other processes when it may take longer to execute.

Ex: You are making a Crawler web where you will read html information and for each page read, you will have to download 10 photos. See the download process is a great candidate to be the async process, as the download will depend on several factors such as your internet, server internet, etc. So it might be the bottleneck. If you do several Task (read pages) and each page download synchronously, it will read several pages at the same time, however photos will be one at a time for each process, and not the 10 photos at the same time. So this process of choosing what makes something async really complex and needs a good understanding of the whole environment.

  1. My suggestion is to determine what really needs to be Async and focus on it.

Generic example of an Async multi task console program

static void Main(string[] args)
{
      TarefaBaseAsync().Wait();
}


private static async Task TarefaBaseAsync()
{
    var tarefas = MeuIENumerable.Select((registro, index) =>
    {
         return Task.Run(async () => await TarefaSecundaria(registro, index));
    });

    await Task.WhenAll(tarefas.ToArray());
}


public static async Task TarefaSecundaria(string valor, int index)
{
               //posso ter tarefas sincronas que eu acredito que não serão gargados
         InsereDapperRegistros();

         // para processos que acho que possa ser um gargalo posso usar o async 
         await DownloadFotoAsync(algumvalor, index);
}

More information on: What is the difference between async, multithereading, parallelism and competition?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.