title: Only one async task operation at a time per a symbol
body: <p><code>SubscribeToKlineUpdatesAsync</code> is a web socket stream event handler receiving data on each second from <a href="https://www.binance.com/en" rel="nofollow noreferrer">Binance</a>. I have a long-running operation for 10 seconds inside it. It's separated into a task, because I don't want it to block any incoming price data.</p>
  <p>The problem is that it spawns thousand of tasks at a time and I want it to spawn only one task at a time per a symbol. The symbol is identified by <code>data.Symbol</code>. Below the code, you'll find the logs. The second log is what I want it to be like.</p>
  <p>I would like to use something like TPL Dataflow or Rx.NET. I just don't know how to do it. I'm aware of the <code>lock</code> way, <code>BlockingCollection</code> and <code>SemaphoreSlim</code>.</p>
  <p>There is a log below of what's current and what I want to accomplish.</p>
  <h3>Code</h3>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Threading;
  using System.Threading.Tasks;
  using Binance.Net;
  using Binance.Net.Enums;
  using Binance.Net.Interfaces;
  using Binance.Net.Objects.Spot;
  using CryptoExchange.Net.Authentication;
  using CryptoExchange.Net.Logging;
  
  namespace SubscribeToCandlesEventFixTest
  {
      public class BinanceSocketHandler
      {
          private readonly IBinanceClient _client;
          private readonly IBinanceSocketClient _socketClient;
  
          public BinanceSocketHandler()
          {
              _client = new BinanceClient(new BinanceClientOptions
              {
                  ApiCredentials = new ApiCredentials(&quot;not required&quot;, &quot;not required&quot;),
                  AutoTimestamp = true,
                  AutoTimestampRecalculationInterval = TimeSpan.FromMinutes(30),
  #if DEBUG
                  LogVerbosity = LogVerbosity.Debug
  #endif
              });
  
              _socketClient = new BinanceSocketClient(new BinanceSocketClientOptions
              {
                  ApiCredentials = new ApiCredentials(&quot;not required&quot;, &quot;not required&quot;),
                  AutoReconnect = true,
                  ReconnectInterval = TimeSpan.FromSeconds(15),
  #if DEBUG
                  LogVerbosity = LogVerbosity.Debug
  #endif
              });
          }
          
          public Task StartAsync(CancellationToken cancellationToken)
          {
              var symbols = new[] { &quot;TRXUSDT&quot;, &quot;BTCUSDT&quot; };
              var interval = KlineInterval.OneMinute;
  
              return _socketClient.Spot.SubscribeToKlineUpdatesAsync(symbols, interval,
                  data =&gt;
                  {
                      if (data.Data.Final)
                      {
                          Console.WriteLine($&quot;[{DateTime.UtcNow}] [{data.Symbol}] New final candle | Timestamp: {data.Data.OpenTime} | Price: {data.Data.Close}&quot;);
                      }
                      else
                      {
                          Console.WriteLine($&quot;[{DateTime.UtcNow}] [{data.Symbol}] Candle update | Timestamp: {data.Data.OpenTime} | Price: {data.Data.Close}&quot;);
  
                          Task.Run(async () =&gt;
                          {
                              Console.WriteLine(&quot;Operation taking 10 seconds to execute...&quot;);
  
                              await Task.Delay(10000, cancellationToken).ConfigureAwait(false);
                          }, cancellationToken);
                      }
                  });
          }
      }
  
      class Program
      {
          static async Task Main(string[] args)
          {
              var test = new BinanceSocketHandler();
              await test.StartAsync(new CancellationToken()).ConfigureAwait(false);
  
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  <h3>Log</h3>
  <p>It spawns thousand of tasks. I want only one task per a symbol.</p>
  <pre><code>[3/7/2021 12:24:53 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 0.05049000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:24:53 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49240.17000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:24:55 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49245.66000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:24:57 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49251.02000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:24:59 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49254.44000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:00 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 0.05049000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:01 AM] [BTCUSDT] New final candle | Timestamp: 3/7/2021 12:24:00 AM | Price: 49257.99000000
  [3/7/2021 12:25:01 AM] [TRXUSDT] New final candle | Timestamp: 3/7/2021 12:24:00 AM | Price: 0.05049000
  [3/7/2021 12:25:03 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 49264.26000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:04 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 0.05049000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:05 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 49266.60000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:07 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 49251.02000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:07 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 0.05050000
  Operation taking 10 seconds to execute...
  </code></pre>
  <h3>Example log of what I want</h3>
  <pre><code>[3/7/2021 12:24:53 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 0.05049000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:24:53 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49240.17000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:24:55 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49245.66000000
  [3/7/2021 12:24:57 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49251.02000000
  [3/7/2021 12:24:59 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 49254.44000000
  [3/7/2021 12:25:00 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:24:00 AM | Price: 0.05049000
  [3/7/2021 12:25:01 AM] [BTCUSDT] New final candle | Timestamp: 3/7/2021 12:24:00 AM | Price: 49257.99000000
  [3/7/2021 12:25:01 AM] [TRXUSDT] New final candle | Timestamp: 3/7/2021 12:24:00 AM | Price: 0.05049000
  [3/7/2021 12:25:03 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 49264.26000000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:04 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 0.05049000
  Operation taking 10 seconds to execute...
  [3/7/2021 12:25:05 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 49266.60000000
  [3/7/2021 12:25:07 AM] [BTCUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 49251.02000000
  [3/7/2021 12:25:07 AM] [TRXUSDT] Candle update | Timestamp: 3/7/2021 12:25:00 AM | Price: 0.05050000
  </code></pre>