I am a beginer in programing.
When i execute the code by locking the operation:
class ThreadSafe
{
static List<string> list = new List<string>();
static object obj=new object();
static void Main()
{
new Thread(AddItems).Start();
new Thread(AddItems).Start();
foreach (string str in list)
{
Console.WriteLine(str);
}
Console.WriteLine("Count=" + list.Count.ToString());
Console.ReadKey(true);
}
static void AddItems()
{
lock (obj)
{
for (int i = 1; i < 10; i++)
list.Add("Item " + i.ToString());
}
}
}
even i am reciving,"InvalidOperationException".What would be the code alteration?