title: How to add item to list using a thread
body: <p>I have a list of reservations, but some ones has a conflict (they are booked in the same day for the same room). In order to solve this problem, I want to create a new list without conflicts. I can change only the room number, but not the reservation date, so I do that in the following method:</p>
  
  <pre><code>        public List&lt;Reservation&gt; ResolveConflicts()
          {
              var groupedReservationByDate = _reservations.GroupBy(reservation =&gt; reservation.CheckInDate);
              var resolvedReservations = new List&lt;Reservation&gt;();
              foreach (var reservations in groupedReservationByDate)
              {
                  foreach (var currentReservation in reservations)
                  {
                      var nextReservations = reservations.Skip(reservations.ToList().IndexOf(currentReservation) + 1)
                          .ToList();
                      foreach (var nextReservation in nextReservations)
                      {
                          if (currentReservation.RoomNumber == nextReservation.RoomNumber)
                          {
                              var thread = new Thread(() =&gt; ChangeRoomNumber(reservations, currentReservation));
                              thread.Start();
                          }
                      }
  
                      resolvedReservations.Add(currentReservation);
                  }
              }
  
              return resolvedReservations;
          }
  
          private void ChangeRoomNumber(IEnumerable&lt;Reservation&gt; reservations, Reservation currentReservation)
          {
              var roomNumber = GetRoomNumber();
              foreach (var reservation in reservations)
              {
                  if (roomNumber == reservation.RoomNumber)
                      roomNumber = GetRoomNumber();
              }
  
              currentReservation.RoomNumber = roomNumber ;
          }
  
          private int GetRoomNumber()
          {
              var randomNumber = new Random();
              return randomNumber.Next(1, _reservations.MaxBy(reservation =&gt; reservation.RoomNumber).RoomNumber);
          }
  </code></pre>
  
  <p>When I do that with a thread, I get a kind of correct list, but is not correct, that returned list should have no conflicts. The same output is produced when I use Thread.Sleep(1000) <a href="https://i.stack.imgur.com/uAloI.png" rel="nofollow noreferrer">Console output with reservation conflicts using a thread</a>.</p>
  
  <p>And if I don't use a thread, the returned list is totally incorrect, for the same room are a lot of conflicts <a href="https://i.stack.imgur.com/rtLMy.png" rel="nofollow noreferrer">Console output with reservation conflicts without using a thread</a>.</p>
  
  <p>I think is a problem with the thread, the algorithm is correct or at least I hope is correct.</p>