I've got an issue which is probably easily solvable, but for some reason I can't wrap my head around...
I have a single list, which contains a class which contains some information. One of these is an ID, which starts from 0 and increases by one with each submission.
When running multiple threads, they submit a different variation of the same ID. This should not be possible, as it checks whether it can be added just before I literally call the List<>().Add.
Any suggestions on how I can avoid this?
Main method:
public static bool AddToList(List<ExampleItem> itemList, List<Xxx> xxx, ExampleItem newItem)
{
ExampleItem lastItem = itemList[itemList.Count - 1];
// We must validate the old item one more time before we progress. This is to prevent duplicates.
if(Validation.ValidateIntegrity(newItem, lastItem))
{
itemList.Add(newItem);
return true;
}
else
return false;
}
Validation method:
public static bool ValidateBlockIntegrity(ExampleItem newItem, ExampleItem lastItem)
{
// We check to see if the ID is correct
if (lastItem.id != newItem.id - 1)
{
Console.WriteLine("ERROR: Invalid ID. It has been rejected.");
return false;
}
// If we made it this far, the item is valid.
return true;
}