1
Vote

Reading 2 excel files simultaneously gives an exception

description

I am reading two excel files simultaneously. Here is my Code
Parallel.Invoke(
                ()=> 
                {
                    FileStream stream = File.Open(@"C:\Projects\TPD.xlsx", FileMode.Open, FileAccess.Read);
                    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }, 
                () => 
                {
                    FileStream stream = File.Open(@"C:\Projects\TPD1.xlsx", FileMode.Open, FileAccess.Read);
                    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }); 
I am getting an Exception (An item with the same key already added) in Class LogManager Method Log, where it is trying to add a new logger in the dictionary. This issue is related to synchronization and proposed code to solve this problem is
public static ILog Log(string objectName)
        {
            ILog result = null;
            lock (_sync)
            {
                if (_dictionary.ContainsKey(objectName) == false)
                {
                    _dictionary.Add(objectName, Excel.Log.Log.GetLoggerFor(objectName));
                }
                result = _dictionary[objectName];

            }
            return result;
        }

file attachments

comments