Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
How to parallellize/async this method?

I am trying to write this code in a way where it doesn't block the program. client.Spot.Market.GetKlinesAsync supports await but not sure how I can structure the code so that it can be awaited.

public static async Task GetMinMaxValues ( BinanceClient client, ObservableCollection<Coin> coins )
{
    foreach ( var coin in coins )
    {
        var task = client.Spot.Market.GetKlinesAsync ( coin.SymbolPair, KlineInterval.OneDay );
        coin.MinVal = klines.Result.Data.Select ( k => k.Low ).Min ( );
        coin.MaxVal = klines.Result.Data.Select ( k => k.High ).Max ( );
    }
}

What confuses me is how I can achieve this because I need to be able to compute the min and max values. So if I access Result, the function is executed immediately, right?

So I can't get my head around on how to achieve this computation in parallel.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Just create nested async operation to get min/max for single Coin, then run all in parallel:

public static async Task GetMinMaxValues ( BinanceClient client, 
                                     ObservableCollection<Coin> coins )
{
    await Task.WhenAll(coins.Select(async c=>await GetMinMax(c, client)).ToList());
}

public static async Task GetMinMax(Coin coin, BinanceClient client)
{
    var res = await client.Spot.Market.GetKlinesAsync ( coin.SymbolPair );
    coin.MinVal = klines.Result.Data.Select ( k => k.Low ).Min ( );
    coin.MaxVal = klines.Result.Data.Select ( k => k.High ).Max ( );
}

The danger of such a approach may be that in case when you have really many Coins, you will create a big bunch of task at the single moment. If this is a case, then you can additionally perform some kind of partition of the work to the chunks of the appropriate size.

over 4 years ago · Santiago Trujillo Relatório

0

You don't need to do it in parallel to not block the UI. Just await the asynchronous method call. It's unclear exactly what GetKlinesAsync returns, but something like this would work without blocking the UI (assuming the method is IO-bound and assuming your calculations are reasonably fast):

public static async Task GetMinMaxValues(BinanceClient client, ObservableCollection<Coin> coins)
{
    foreach (var coin in coins)
    {
        //Invokes asynchronous get method, waits for it to finish, and gets the result
        var result = await client.Spot.Market.GetKlinesAsync(coin.SymbolPair, KlineInterval.OneDay);
        //Use the result to set your coin values
        coin.MinVal = result.Data.Select(k => k.Low).Min();
        coin.MaxVal = result.Data.Select(k => k.High).Max();
    }
}

If you wanted to run the calls in parallel to speed up the execution, you could, but this is not necessary to prevent the method call from blocking the calling thread.

Consider reading the Microsoft documentation on Task-based asynchronous programming. There are many gotchas ahead if you don't truly understand how Tasks, async and await work at a basic level.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda