Transform Synchronous Method to Asynchronous

2

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 multiple items in 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 was not using the await operator in an asynchronous method. But basically I'm reusing the existing method and creating an asynchronous alternative that I call the following way:

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

I used a% w of% for the parameter only as a simplified example.

     

Synchronous method should continue to exist in the same way.


1. This way the objects in the list will in fact be processed     same time?

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

    
asked by anonymous 25.07.2017 / 15:20

1 answer

0
  • 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, so the important thing in an async process is to isolate it from other processes when it may take longer to execute.

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

  • My suggestion is to determine what Async really needs to be 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 at: What difference between async, multithereading, parallelism and competition?

        
    22.08.2017 / 18:17