title: Missing rows from list when using dynamic task in dotnet
body: <p>I am new in using <code>TPL</code> in .Net applications. While creating a simple console application to achieve some parallel tasks those are dynamically created, I am stuck with some issues.  </p>
  
  <p>Problem here is that when 10 tasks are created and run, although the console is showing all the 10 tasks, when writing those into a log file after putting a delay between consoling and logging, the log file misses some of the items randomly.</p>
  
  <p>Below is my sample code (This is just a skeleton of my actual code)</p>
  
  <pre><code>class Program
  {
      public static int datacount = 10;
  
      static void Main(string[] args)
      {
          List&lt;Task&gt; tasks = new List&lt;Task&gt;();
          var s1 = DateTime.Now;
          var transList = GenerateTransactionList();
  
          foreach (var transaction in transList)
          {
              Transactions transactionNew = new Transactions();
              transactionNew = transaction;
              tasks.Add(Task.Factory.StartNew(() =&gt; serialMethod(transactionNew)));
  
          }
          Task.WhenAll(tasks).Wait();
          Console.WriteLine("Completed!!!");
      }
  
      private static List&lt;Transactions&gt; GenerateTransactionList()
      {
          Random r = new Random();
          List&lt;Transactions&gt; transactionList = new List&lt;Transactions&gt;();
          for (int i = 1; i &lt;= datacount; ++i)
          {
              Transactions tr = new Transactions();
              tr.ID = 0;
              tr.Amount = r.Next(1, 10);
              tr.Created_By = "Iteration" + i;
              tr.Notes = "Iteration" + i;
              tr.Created_On = DateTime.Now;
              transactionList.Add(tr);
          }
          return transactionList;
      }
      private static async Task&lt;string&gt; serialMethod(Transactions tlist)
      {
          Console.WriteLine("Started Serial Iteration" + tlist.Notes);
          try
          {
              Console.WriteLine("Finished Serial Iteration" + tlist.Notes);
              Thread.Sleep(10000);//doing some time consuming process
              WriteLog("Parallel2", DateTime.Now, DateTime.Now, tlist.Notes);
  
              return "Success";
          }
          catch (Exception ex)
          {
              Console.WriteLine("serialmethod" + ex.Message);
              return "Failure";
          }
      }
  
      public static void WriteLog(string type,
          DateTime startTime, DateTime endTime,
          string dataSet)
      {
          try
          {
              string logFolderPath = AppDomain.CurrentDomain.BaseDirectory + @"\Logs";
  
              if (!Directory.Exists(logFolderPath))
                  Directory.CreateDirectory(logFolderPath);
  
              string logFilePath = logFolderPath + @"\Log_" + DateTime.Today.ToString("yyyy.MM.dd") + ".csv";
  
              string line = string.Empty;
              if (!File.Exists(logFilePath))
              {
                  line = @"""Type"",""Start Time"",""End Time"",""Duration"",""Iteration""";
                  writeLineToFile(logFilePath, line);
              }
  
              string duration = (endTime - startTime).ToString();
  
              line = "\"" + type + "\"," +
                     "\"" + startTime.ToString("MM/dd/yyyy hh:mm:ss tt") + "\"," +
                     "\"" + endTime.ToString("MM/dd/yyyy hh:mm:ss tt") + "\"," +
                      "\"" + duration + "\"," +
                     "\"" + dataSet + "\"";
  
              writeLineToFile(logFilePath, line);
  
          }
          catch (Exception)
          {
              //do nothing
          }
      }
  
      private static void writeLineToFile(string fileName, string line)
      {
  
          using (var writer = new StreamWriter(fileName, true))
          {
              writer.WriteLine(line);
          }
      }
  }
  
  class Transactions
  {
      public int ID { get; set; }
      public decimal Amount { get; set; }
      public int Points { get; set; }
      public string Notes { get; set; }
      public string Created_By { get; set; }
      public DateTime Created_On { get; set; }
  
  }
  </code></pre>
  
  <p>Do you have any idea why this is happening. I have tried using <code>ConcurrentBag</code> instead of list. But that too is not helping. Please guide and let me know if I am missing anything or my implementation is completely wrong.</p>