title: Combining a while loop with Task.Run() in C#
body: <p>I'm pretty new to multithread applications in C# and I'm trying to edit my code below so that it runs on multiple threads. Right now it operates synchronously and it takes up very little cpu power. I need it to run much faster on multiple threads. My thought was starting a task for each core and then when a task finishes, allow another to take its place or something like that if it is possible.</p>
  
  <pre><code>static void Main(string[] args)
      {
          string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
          QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "OoplesQueue");
  
          try
          {
              while (true)
              {
                  Task.Run(() =&gt; processCalculations(Client));
              }
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
              Console.WriteLine(ex.StackTrace);
          }
      }
  
      public static ConnectionMultiplexer connection;
      public static IDatabase cache;
  
      public static async Task processCalculations(QueueClient client)
      {
          try
          {
              BrokeredMessage message = await client.ReceiveAsync();
  
              if (message != null)
              {
                  if (connection == null || !connection.IsConnected)
                  {
  
                      connection = await ConnectionMultiplexer.ConnectAsync("connection,SyncTimeout=10000,ConnectTimeout=10000");
                      //connection = ConnectionMultiplexer.Connect("connection,SyncTimeout=10000,ConnectTimeout=10000");
                  }
  
                  cache = connection.GetDatabase();
  
                  string sandpKey = message.Properties["sandp"].ToString();
                  string dateKey = message.Properties["date"].ToString();
                  string symbolclassKey = message.Properties["symbolclass"].ToString();
                  string stockdataKey = message.Properties["stockdata"].ToString();
                  string stockcomparedataKey = message.Properties["stockcomparedata"].ToString();
  
                  List&lt;StockData&gt; sandp = cache.Get&lt;List&lt;StockData&gt;&gt;(sandpKey);
                  DateTime date = cache.Get&lt;DateTime&gt;(dateKey);
                  SymbolInfo symbolinfo = cache.Get&lt;SymbolInfo&gt;(symbolclassKey);
                  List&lt;StockData&gt; stockdata = cache.Get&lt;List&lt;StockData&gt;&gt;(stockdataKey);
                  List&lt;StockMarketCompare&gt; stockcomparedata = cache.Get&lt;List&lt;StockMarketCompare&gt;&gt;(stockcomparedataKey);
  
                  StockRating rating = performCalculations(symbolinfo, date, sandp, stockdata, stockcomparedata);
  
                  if (rating != null)
                  {
                      saveToTable(rating);
                      if (message.LockedUntilUtc.Minute &lt;= 1)
                      {
                          await message.RenewLockAsync();
                      }
                      await message.CompleteAsync();
                  }
                  else
                  {
                      Console.WriteLine("Message " + message.MessageId + " Completed!");
                      await message.CompleteAsync();
                  }
              }
          }
          catch (TimeoutException time)
          {
              Console.WriteLine(time.Message);
          }
          catch (MessageLockLostException locks)
          {
              Console.WriteLine(locks.Message);
          }
          catch (RedisConnectionException redis)
          {
              Console.WriteLine("Start the redis server service!");
          }
          catch (MessagingCommunicationException communication)
          {
              Console.WriteLine(communication.Message);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
              Console.WriteLine(ex.StackTrace);
          }
      }
  </code></pre>