I have 2 main threads to my program:
Thread checkForNewItemsThread
Adds new objects to avolatile List<Object>
Thread workOnListThread
Works on theList<Object>
and creates another newThread doWorkThread
for each item in list that needs work.
I am running into an ArgumentNull Exception during my workOnListThread
because (what I think is happening) either:
- My
checkForNewItemsThread
is adding to the list whileworkOnListThread
is iterating through the list. - My
doWorkThread
is removing the item from the list once it is finished working on that object.
I believe the right hing to do here would be to put a lock on the List<Object>
while workOnListThread
is working on the list, however, I'm having a hard time determining the right place to place the lock on the List<Object>
, I think this should be correct, however could use some guidance.
volatile List<Object> List1 = new List<Object>();
private static readonly object _lock = new object();
Thread checkForNewItemsThread;
Thread workOnListThread;
Thread doWorkThread;
public void OnTimerCheckNewItems(object sender, ElapsedEventArgs args)
{
List<Object> List1Temp = new List<Object>();
List1Temp = getList();
addNewItems(List1Temp);
}
private void addNewItems(List<Object> list)
{
foreach (Object obj in list)
{
if (!List1.Any(o => o.ID == obj.ID)) //check if object already exists
{
List1.Add(obj);
}
}
}
private void workOnList()
{
while (true) //loop forever
{
while (List1.Count != 0) //while items exist
{
lock (_lock) //lock while iterating through list
{
try
{
for (int i = 0; i < List1.Count; i++)
{
if (List1[i].Status == Status.ready && List1[i] != null)
{
doWorkThread = new Thread(() => doWork(List1[i]));
doWorkThread.Start();
Thread.Sleep(3000); // wait for process to start
}
}
}
catch (Exception e)
{
Console.WriteLine(DateTime.Now.ToString(), " : ", e);
Log(e.ToString());
}
}
}
}
}
private void doWork(Object obj)
{
lock (_lock) //lock during update to status
{
int i = List.IndexOf(obj);
List1[i].Status = Status.pending;
}
//work done here
List1.Remove(alert);
}