I am getting:
“collection was modified enumeration operation may not execute” at this lline:
foreach (Message msg in queue)
after a while.
I have to use .NET 2.0.
The two operations I make on the private List<> named “queue” are as follows:
// 1st function calls this
lock (queue)
{
queue.Add(msg);
}
// 2nd function calls this
lock (queue)
{
using (StreamWriter outfile = new StreamWriter("path", true)
{
foreach (Message msg in queue) // This is were I get the exception after a while)
{
outfile.WriteLine(JsonConvert.SerializeObject(msg));
}
queue = new List<Message>();
}
}
What am I doing wrong?
the parameter which is used by the lick statement should be readonly. See this link
Use
readonly private objectinstead ofqueqeCode should be