I was working on a project written in C# where I had couple of collections that would be accessed and modified by different threads/tasks, therefore I used the lock keyword(quite a lot). I followed the notes mentioned here and other references/tutorials available online to utilize this keyword properly. At the exam, I was asked how I could avoid these explicit locks. Thanks in advance and I hope this question is proper to be asked here.
Here is some code sample(there are other loops and method calls as well, I deleted them to have a shorter sample) :
try
{
lock (this)
{
if (!IsReplaying)
{
//removing igonored tracks from bufferlist
for (int i = 0; i < BufferList.Count; i++)
{
for (int j = 0; j < ListOfIgnoredTracks.Count; j++)
{
CAT62Data dataitem = BufferList[i];
if (BufferList[i].CAT62DataItems[10].value != null)
{
if (BufferList[i].CAT62DataItems[10] != null && BufferList[i].CAT62DataItems[10].value != null)
{
if (dataitem.CAT62DataItems[10].value.ToString() == ListOfIgnoredTracks[j].CAT62DataItems[10].value.ToString() && !dataitem.IsModified && !dataitem.IsUserAdded)
{
BufferList.RemoveAt(i--);
}
}
}
}
}
BufferList.Clear();
RemoveLostTracks(new DateTime());
}
}
}